This is meant to be a basic calculator. Can anyone help me to define 'ans' in my code. It says error on line 14. I'm very new to python and don't know how and I'm very stuck. I also need to make sure that whatever the answer to the sum is will be carried over into the next sum the next time I call the function.
def rep(num1,ans):
num2 = int(input("Next number? "))
choice = input("select operation -,+,x,/. ")
if (choice == "+"):
ans= (num1+num2)
elif (choice == "-"):
ans= (num1-num2)
print (ans)
num1 = int(input("First number? "))
rep(num1, ans)
morenum = ("yes")
morenum = input("Do you want to use another number? ")
while (morenum == "yes"):
rep(ans, num1)
4 Answers 4
You are using ans before you define it: rep(num1, ans)
Initialize ans like this instead:
ans = int(input("First number? "))
morenum = "yes"
while (morenum == "yes"):
morenum = input("Do you want to use another number? ")
rep(ans, num1)
Note that morenum = input("Do you want to use another number? ") needs to be in the loop or it will not be called every time you need it.
There is also the issue that the answer is not being returned from the function and therefore does not update as it should, but that is left as an exercise for the OP.
Comments
I see a couple different problems with this code. Nothing that can't be fixed, though!
First of, be mindful of spacing/indentation. Python uses it to know how to 'read' your program and telling what lines of code goes with others. I'm sure some helpful user on here will edit your original q to have proper indentation :-)
def rep(num1,ans):
num2 = int(input("Next number? "))
choice = input("select operation -,+,x,/. ")
if (choice == "+"):
ans = (num1+num2)
elif (choice == "-"):
ans = (num1-num2)
elif (choice == "x"):
ans = (num1*num2)
elif (choice == "/"):
ans = (num1/num2)
print (ans)
num1 = int(input("First number? "))
rep(num1, ans)
This is your first block of code, where you define the rep function with some fixed spacing. The problem is that you're passing ans in as an argument without ever defining it. Just remove it as an argument, and instead have ans get returned by the rep:
def rep(num1):
num2 = int(input("Next number? "))
choice = input("select operation -,+,x,/. ")
if (choice == "+"):
ans= (num1+num2)
elif (choice == "-"):
ans= (num1-num2)
elif (choice == "x"):
ans= (num1*num2)
elif (choice == "/"):
ans= (num1/num2)
return ans
num1 = int(input("First number? "))
rep(num1)
On to the 2nd block:
morenum = ("yes")
morenum = input("Do you want to use another number? ")
while (morenum == "yes"):
rep(num1)
This is where your code gets a little wonky. You initially define the morenum variable, then replace it with an input. Also, this won't work the way you think it will - I think you're under the impression that this will ask the user if they want another number after each time they complete a math operation. To achieve this, use this code instead:
while True:
morenum = input("Do you want to use another number? ")
if morenum == 'yes':
num1 = int(input("First number? "))
rep(num1)
else:
quit()
Comments
You are trying to pass a variable ans to the function rep before its ever initialized in this scope. Try this:
num1 = int(input("First number? "))
ans=0
rep(num1, ans)
Or you could simply call the rep method like this rep(num1, None) or maybe like this rep(num1, 0) and it will work.
Although a better method would be:
def rep(num):
num_two = int(input("Next number? "))
choice = input("select operation -,+,x,/. ")
if choice == "+":
ans = num+num_two
elif choice == "-":
ans= num-num_two
elif choice == "x":
ans= num*num_two
elif choice == "/":
ans= num/num_two
print(ans)
num = int(input("First number? "))
rep(num)
Comments
Well, to be honest, I don't know why you are having ans as a parameter in your code. If I were you, I'd just do it like this:
def rep():
goes+=1
firstnum = int(raw_input("first number?") #I am guessing you're using python 2.7
secondnum = int(raw_input("second number?")
operation = raw_input("operation")
if operation=="+":
sum = firstnum+secondnum
return sum
elif operation=="-":
diff = firstnum-secondnum
return diff
elif operation=="*":
product = firstnum*secondnum
return product
elif operation=="/":
quo = float(firstnum)/float(secondnum) #to make sure there's no rounding
return quo
else:
raise ValueError("Improper entry!")
This new function can be called directly like this:
result = rep()
Comments
Explore related questions
See similar questions with these tags.