Python“while-loop”问题

时间:2014-02-11 20:42:52

标签: python syntax while-loop

我写了这个Python脚本:

# random.py    This program displays a random number
#

import random

fl_running_0 = True

while fl_running_0:
    fl_running_1 = True

    print(str(random.choice([1, 2, 3, 4, 5, 6]))

    while fl_running_1 == True:
        str_input = input('Do you want to display another number? (yes/no) ')

        if str_input == 'yes' or str_input == 'y':
            fl_running_0 = True              # Optional -> just for readability
            fl_running_1 = False
        elif str_input == 'no' or str_input == 'n':
            fl_running_0 = False
            fl_running_1 = False
        else:
            print('Unexpected user input...try again!')
            fl_running_1 = True

当我尝试运行它时出现错误(“无效语法”),我无法弄清楚原因。 我错了什么?

问候, Lackmeier

1 个答案:

答案 0 :(得分:4)

您在上面一行中缺少右括号:

print(str(random.choice([1, 2, 3, 4, 5, 6])))
#                                     here--^