【问题标题】:What am i doing wrong? (Python)我究竟做错了什么? (Python)
【发布时间】:2016-07-18 10:53:16
【问题描述】:

我正在尝试制作一个基本的数字猜谜游戏,因为我刚刚开始编码。我不明白我做错了什么。我试着在这里的论坛上寻找,但我没有找到答案。如果有人能告诉我我做错了什么,我将不胜感激!

import random
num = random.randint(1, 10)

while true:
    guess = int(input("Guess a number between 1 and 10: "))

    if guess is == num
        print("you got it!")
            break

    else:
        print("try again!")

错误:

break
    ^
IndentationError: unexpected indent

【问题讨论】:

  • clue if guess is == num 也没有意义,如果您收到错误然后包含错误。您可能希望 if guess == num: 注意删除 is 并添加冒号 :
  • 谢谢你我修正了那个错误。你介意解释为什么结肠是必要的吗?我现在有一个不同的错误:break ^ IndentationError: unexpected indent
  • 咨询docs
  • 好的,感谢您的帮助。

标签: python


【解决方案1】:

应该是if guess == num:。你不需要is,你需要把:放在if上。此外,true 应该是 True 并且 break 必须具有正确的标识。

import random
num = random.randint(1, 10)

while True:
    guess = int(input("Guess a number between 1 and 10: "))

    if guess == num:
        print("you got it!")
        break

    else:
        print("try again!")

【讨论】:

    猜你喜欢
    • 2013-08-06
    • 1970-01-01
    • 2019-12-23
    • 2014-06-15
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多