python文本菜单程序_Python文本菜单无限循环

而不是在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值