jediquest1
jediquest1

Reputation: 7

Why wont my simple python adding calculator work?

Hello I was wondering why my code for an adding calculator wasn't working, I am new to python and programing in general, I am using python 3.4.1.Heres the code:

def add(x, y):
    """This function will add two numbers"""

    return x + y


number1 = int(input("pick a number: "))

number2 = int(input("pick another number: "))

choice = input
choice2 = input 
if choice == '1' '2' '3' '4' '5' '6' '7' '8' '9' '10':
    print (number1,"+",number2, "=", add(number1,number2))

if choice2 == '1' '2' '3' '4' '5' '6' '7' '8' '9' '10':
    print (number1,"+",number2, "=", add(number1,number2))

EDIT: thank you everyone for the help I figured it out :)

Upvotes: 0

Views: 283

Answers (4)

Stefan Wow
Stefan Wow

Reputation: 1

You could have a calculator where you choose any two numbers and then choose what you want to do to them:

num_1 = int(raw_input("Enter Your First Number"))
num_2 = int(raw_input("Enter Your Second Number"))
sum_1 = raw_input("Choose Sum (+ - / or *)")

if sum_1 == "+":
  print num_1, "+", num_2, "=", num_1 + num_2
elif sum_1 == "-":
  print num_1, "-", num_2, "=", num_1 - num_2
elif sum_1 == "/":
  print num_1, "/", num_2, "=", num_1 / num_2
elif sum_1 == "*":
  print num_1, "*", num_2, "=", num_1 * num_2
else:
  print "Numbers or Sum Not Recognised"

Upvotes: 0

Weafs.py
Weafs.py

Reputation: 22992

Here,input is not defined, so it will throw an error input undefined

choice = input
choice2 = input 

You can't do it this way:

if choice == '1' '2' '3' '4' '5' '6' '7' '8' '9' '10':
    print (number1,"+",number2, "=", add(number1,number2))

if choice2 == '1' '2' '3' '4' '5' '6' '7' '8' '9' '10':
    print (number1,"+",number2, "=", add(number1,number2))

The correct way to do it would be:

if int(choice) <= 10:
    # do something

This is how you should do it, if you want to limit the numbers from 1 to 10.

num1 = int(input("Pick a number: "))
num2 = int(input("Pick another number: "))

if num1 <= 10 and num2 <= 10:
    print(str(num1) + "+" + str(num2) "=", str(sum([num1, num2])))
else:
    print("The number you entered is greater than 10")

If you are not familiar with the sum() function,sum([num1, num2]) returns the sum of num1 and num2.

And this is for if you want to put no limit:

num1 = int(input("Pick a number: "))
num2 = int(input("Pick another number: "))

print("The sum of " + num1 + "and " + num2 + "is: " + str(sum([num1 + num2])))

Upvotes: 1

000
000

Reputation: 27227

It looks like you want the user to input two numbers, then you validate that both of those numbers are between 1 and 10, and if they are, you output the addition string.

In that case: The line int(input("pick a number: ")) already guarantees that the input will be a number. The function int turns the input into a number.

So all that's left is to check that both inputs are between 1 and 10.

def add(x, y):
    """This function will add two numbers"""
    return x + y

number1 = int(raw_input("pick a number: "))

number2 = int(raw_input("pick another number: "))

if 1 <= number1 <= 10 and 1 <= number2 <= 10:
    print (number1,"+",number2, "=", add(number1,number2))

If you really want to be nice to the user, you can validate their input as you go along:

number1 = -1
while not 1 <= number1 <= 10:
  number1 = int(raw_input("pick a number: "))

number2 = -1
while not 1 <= number2 <= 10:
  number2 = int(raw_input("pick another number: "))

print (number1,"+",number2, "=", add(number1,number2))

Upvotes: 0

Amadan
Amadan

Reputation: 198324

In Python, 'a' 'b' is the same as 'ab'.

Thus, your code is asking for strings, converting them to integers, then asking for another two strings, which it compares to '12345678910'. Unless you enter '12345678910' for one of them, nothing happens.

Unfortunately, aside from that, your question is a bit vague. If you describe what exactly the program is supposed to do ("a calculator" is not nearly specific enough), we might help you work out how to do it.

EDIT: too much Ruby, didn't even notice the choice = input thing. See rroszkowiak's answer.

Upvotes: 0

Related Questions