简单的Python游戏while循环语法错误

时间:2017-09-13 11:59:31

标签: python

所以 - 我是Python的新手,只是乱搞一些基本游戏来了解语言。无论出于何种原因,这个小代码返回“第8行的无效语法”,这是“虽然猜错了== 1“线......任何人都可以告诉我为什么?

from random import randint

UserResponse = int(input("Guess what number between 1 and 100 I'm thinking 
of!"))
RandomNumber = randint(1,100)
guessedwrong = 1

While guessedwrong == 1:
    If UserResponse > RandomNumber:
        print("Nope! Lower!")
        UserResponse = int(input("Try again!"))
    elif UserResponse < RandomNumber:
        print("Njet! Higher!")
        UserResponse = int(input("Try again!"))
    else
        print("Correct! You're awesome!")
        GuessedWrong = 0

2 个答案:

答案 0 :(得分:5)

Python区分大小写:

while guessedwrong == 1:
#^
    if UserResponse > RandomNumber
     #^

    # Some other your code
    else:
    #  ^^
        print("Correct! You're awesome!")
        guessedwrong = 0
        # ^^^^^^

答案 1 :(得分:0)

完整更正的代码

from random import randint

UserResponse = int(input("Guess what number between 1 and 100 I'm thinking of!"))
RandomNumber = randint(1, 100)
guessedwrong = 1

while guessedwrong == 1:
    if UserResponse > RandomNumber:
        print("Nope! Lower!")
        UserResponse = int(input("Try again!"))
    elif UserResponse < RandomNumber:
        print("Njet! Higher!")
        UserResponse = int(input("Try again!"))
    else:
        print("Correct! You're awesome!")
        guessedwrong = 0