-1

我是 Python 新手,我想弄清楚为什么我的程序不会超过初始输入。一切正常,直到我输入一个数字。然后它只是回到程序的开头。循环语句如下所示:

 loop = 1
 choice = 0
 while loop == 1:
     choice = menu()
     if choice == 1:
        (List of commands)
     elif choice == 2:
        (List of commands)  etc...
     elif choice == 5:
        loop = 0

我是一个大菜鸟,所以我敢打赌这是一个荒谬的问题,但我只是难住了!

4

2 回答 2

0

TypeError: can't multiply sequence by non-int of type 'str'意味着你像这样乘以'5'*'5',它们是str但不是int 5*5。

'1'+'1' = '11'但是_1 + 1 = 2

所以你应该将str更改为int

于 2012-06-05T01:30:41.237 回答
0
NO_ACTION, SMTH_ACTION, SMTH_OTHER_ACTION, EXIT_ACTION = 0,1,2,3
choice = NO_ACTION
while True:
    choice = int(menu()) # python strong typed!

    if choice is SMTH_ACTION:
        (List of commands)

    elif choice is SMTH_OTHER_ACTION:
        (List of commands)  etc...

    elif choice is EXIT_ACTION:
       break

不需要退出标志 - 使用break关键字。不要使用“幻数”——定义常量。

于 2012-06-05T01:35:02.083 回答