Reputation: 81
I have an assignment to create a basic calculator program that has addition, subtraction, multiplication, and division as choices. Main should call menu, menu should display choices, and after an answer is displayed the program should return to the menu.
When I run it, the menu displays properly and prompts me to choose the math operation, but when I enter a value (even if it is an invalid number) the program does nothing and returns to the menu.
def add(num1,num2):
num1, num2 = prompt()
return num1 + num2
def subtract(num1,num2):
num1, num2 = prompt()
return num1 - num2
def multiply(num1,num2):
num1, num2 = prompt()
return num1 * num2
def divide(num1,num2):
num1, num2 = prompt()
if num1 == 0 and num2 == 0:
print "Dividing zero by zero is undefined."
if num1 != 0 and num2 == 0:
print "Cannot divide by zero."
return float(num1) / num2
def prompt():
num1 = raw_input("Please enter the first value: ")
num2 = raw_input("Please enter the second value: ")
return (num1,num2)
def menu():
print "Basic Calculator"
print "1 - Add"
print "2 - Subtract"
print "3 - Multiply"
print "4 - Divide"
print "5 - Quit"
operation = int(raw_input("Please enter a value to choose from options above: "))
return operation
def main():
while True:
if menu() == 1:
answer = add(num1,num2)
print answer
elif menu() == 2:
answer = subtract(num1,num2)
print answer
elif menu() == 3:
answer == multiply(num1,num2)
print answer
elif menu() == 4:
answer == divide(num1,num2)
print answer
elif menu() == 5:
print "Thank you for using basic calculator"
print "Goodbye!"
break
else:
print "Invalid input"
if __name__=='__main__':
main()
How do I get the main function to call the add, subtract, multiplication, and division functions, and then break if option 5 is selected to quit?
I'll add that I'm new to python. Thank you for any help you can offer!
Upvotes: 1
Views: 2403
Reputation: 11
print("Simple maths count")
num1 = int(input("What is your number?"))
for i in range (1, 20):
print (num1 * i)
Upvotes: 1
Reputation: 122
You invoked a function with parameters that doesn't exist yet: add(num1, num2).
From the snippet below, num1
and num2
is not yet defined since they are initialized by the function prompt()
, which is called by the other functions(add()
, subtract()
, etc).
def main():
while True:
if menu() == 1:
answer = add(num1,num2)
print answer
Initialize num1
and num2
first before using them as parameters in your subsequent calls. Remove the call to prompt() inside the other functions. Something along the lines of calling prompt()
first before using them in a function call:
def add(num1,num2):
return num1 + num2
...
def main():
while True:
if menu() == 1:
num1, num2 = prompt()
answer = add(num1,num2)
print answer
Basically, modify your thinking a little bit. Parameters that you pass into a function call, in your case num1
and num2
passed into add(num1, num2)
, should have been already initialized. That is, num1
and num2
should already have values before you pass them into another function, say add(num1, num2).
Also, raw_input()
returns a string so you must convert them into numerical values first before you can do mathematical operations.
Upvotes: 1
Reputation: 1457
Your error is in main, change to this:
def main():
while True:
operation = menu()
if operation == 1:
answer = add(num1,num2)
print answer
elif operation == 2:
answer = subtract(num1,num2)
print answer
# And so on
Because you called menu() on every IF you only checked one IF per input.
EDIT
Remove all your inputs to your functions: def add(num1,num2)
should be def add():
because they return values, they don't take values.
Upvotes: 3
Reputation:
I can't reproduce the behaviour you describe. But your code does not work because lines like this
answer = add(num1,num2)
throw an error:
$ python2 so.py
Basic Calculator
1 - Add
2 - Subtract
3 - Multiply
4 - Divide
5 - Quit
Please enter a value to choose from options above: 1
Traceback (most recent call last):
File "so.py", line 58, in <module>
main()
File "so.py", line 39, in main
answer = add(num1,num2)
NameError: global name 'num1' is not defined
Where do you think num1
and num2
come from?
Upvotes: 1