我有一个作业,创建一个基本的计算器程序,有加减,乘法和除法作为选择。Main应该调用菜单,菜单应该显示选项,并且在显示应答之后,程序应该返回菜单。在
当我运行它时,菜单会正确显示并提示我选择数学运算,但是当我输入一个值(即使它是一个无效的数字)时,程序什么也不做,然后返回菜单。在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()
如果选择了选项5退出,如何让主函数调用加、减、乘和除函数,然后中断?在
我要补充一点,我是python新手。谢谢你能提供的任何帮助!在