【问题标题】:Continue outside of loop, dice rolling script在循环外继续,掷骰子脚本
【发布时间】:2021-06-16 16:22:25
【问题描述】:

我正在做一个掷骰子的游戏。以下代码会产生错误,提示“在循环外继续”:

import random

repeat="y" or "yes"

while repeat == "y" or "yes"
  print("Rolling the dice")
  print(random.randint(1,6))

repeat = input("Do you wanna roll again Y/N?").lower()

if repeat =="y" or "yes":
  continue
else:
  print("Thanks for rolling")
  break

为什么会出现这个错误?

【问题讨论】:

  • 我已经从您的屏幕截图中转录了您的代码,以便其他人可以阅读。我们更喜欢将问题中的代码作为文本而不是外部图像。

标签: python dice rolling-computation


【解决方案1】:

你不能同时影响两个值,在你的表达中选择“y”或“Y”。

repeat="y"

while (repeat == "y") or (repeat == "yes"):

你也必须检查你的缩进, repeat = input 必须在循环内,所以每次你都会得到一个新的输入来检查。 最后的打印可以放在循环的最后。

这是一个工作示例,您可以键入以“y”/“Y”开头的任何内容来继续滚动:

import random

repeat="y"

while (repeat[0]) == "y":
    print("Rolling the dice")
    print(random.randint(1,6))
    repeat = input("Do you wanna roll again Y/N?").lower()

print("Thanks for rolling")

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 2012-11-27
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多