【问题标题】:Python "while-loop" issue [closed]Python“while循环”问题[关闭]
【发布时间】:2014-02-11 20:42:51
【问题描述】:

我写了这个 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

当我尝试运行它时出现错误(“无效语法”),但我不知道为什么。 我做错了什么?

您好, 拉克迈尔

【问题讨论】:

  • 至少写下完整的错误以帮助我们帮助您..

标签: python syntax while-loop


【解决方案1】:

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

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

【讨论】:

  • 哦,哇...谢谢。我只是没有注意到它,因为它只是在后一行说语法错误......
  • 如果解释器指出一行中有语法错误,在大多数情况下检查前一行是个好主意。通常它是一个未封闭的 (,{ 或 [ 或类似的东西。
  • 感谢您的友好帮助。我对 Stackoverflow 很陌生。抱歉,如果我没有立即获得有关论坛的所有信息:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 2018-08-15
  • 2020-05-27
相关资源
最近更新 更多