而不是在menu内打印选项,你应该返回它们,因为choice需要menu()的返回值,并且menu没有返回任何内容,所以默认返回值None被赋值每次choice。
def menu():
return "blahblah options"
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1: #this is always None
add(input("Add this: "),input("to this: "))
elif choice == 2:
sub(input("Subtract this: "),input("from this: "))
elif choice == 3:
mul(input("Multiply this: "),input("by this: "))
elif choice == 4:
div(input("Divide this: "),input("by this: "))
elif choice == 5:
loop = 0
您应该从用户那里获取输入,而不是从菜单返回固定值:
def menu():
strs = ('Enter 1 for addition\n'
'Enter 2 for subtaction\n'
'Enter 3 for multiplication\n'
'Enter 4 for division\n'
'Enter 5 to exit : ')
choice = raw_input(strs)
return int(choice)
while True: #use while True
choice = menu()
if choice == 1:
add(input("Add this: "),input("to this: "))
elif choice == 2:
sub(input("Subtract this: "),input("from this: "))
elif choice == 3:
mul(input("Multiply this: "),input("by this: "))
elif choice == 4:
div(input("Divide this: "),input("by this: "))
elif choice == 5:
break