数字猜测程序。问题太高太低了

时间:2016-04-18 04:50:24

标签: python python-3.x if-statement

我是一个非常新的程序员,我很糟糕。我试图做一个数字猜谜游戏,但我遇到了“太高,太低的部分”的问题。我的IDE(PyCharm)一直在说syntax error traceback line 18。我一直在调整那条线但是我被打败了。这是代码。

提前致谢。

import random

print("Guess a number between 1 and 100. ")

randomNum = random.randint(1, 100)
found = False

while not found:
    print("Remember, you can only guess between the numbers 1-100!")

userEst = int(input("Guess here:"))
    if userEst == randomNum:
    print("You guessed right!!!")        
    found=True

if userEst > randomNum:
    print("Too high!")

if userEst < randomNum:
    print("Too low!")

2 个答案:

答案 0 :(得分:1)

请注意,在python中,缩进非常重要。每个代码块都有缩进 详情请见:link。 因此,您需要将代码更改为:

import random

print("Guess a number between 1 and 100. ")

randomNum = random.randint(1, 100)
found = False

while not found:
    print("Remember, you can only guess between the numbers 1-100!")

    userEst = input("Guess here:")
    if userEst == randomNum:
        print("You guessed right!!!")
        found=True
    if userEst > randomNum:
        print("Too high!")

    if userEst < randomNum:
        print("Too low!")

顺便说一下,你不要吮吸。

祝你好运。

答案 1 :(得分:0)

看起来你有一个恼人的缩进错误。 Python对此很挑剔。

if userEst == randomNum:
        print("You guessed right!!!")
        found=True

应该是

if userEst == randomNum:
    print("You guessed right!!!")
    found=True