用户输入比较不起作用

时间:2018-04-08 16:55:36

标签: python python-3.x

当程序进入第一级并且游戏开始时,我输入正确的答案,但它不起作用。第一个引用的答案是" death"。 if else语句应该获取用户的答案,如果正确,则显示消息并继续进入下一级别。但事实并非如此。它显示另一条消息,告诉我我输了。

我不明白发生了什么,特别是因为if else语句会判断用户是否想要查看规则。

为了安全起见,我将以更直截了当的形式写下最后一次澄清。

当我在第一级输入正确答案时,我知道这是正确的答案,它不会显示congrats_message,但会显示loss_message。

welcome_message = "Hello! Welcome to Quote Game by Chandler Morell!"
version = "0.0.1"

congrats_message = "Congratulations! You passed level one!"
loss_message = "Sorry. You did not succeed. One life has been lost."

win_game_message = "Congratulations! You won the game!"
lost_game_message = "You made it to the end, but you did not win. Give it another go!"

lives = 3


def game_won():
    print("")
    print(win_game_message)


def game_lost():
    print("")
    print(lost_game_message)


def update_lives():
    global lives
    lives = lives - 1

    if lives is 0:
        print("You have ran out of lives. Game over.")
        game_lost()
        exit(0)


def view_rules():
    rules = ["1. Do not look up quotes online.", "2. You have three lives.",
             "3. You will have three words to choose from.", "4. Type the WORD not the NUMBER"]
    print("")

    for i in rules:
        print(i)


def ask_view_rules():
    rules_answer = input("Would you like to view the rules? (y/n): ")
    if rules_answer is "y":
        view_rules()
    else:
        print("")
        print("Okay, let's begin!")


def level_one():
    print("Lives: ", lives)
    print("We will start with an easy quote. Fill in the blank.")
    print("")
    choices = ["1. death", "2. torture", "3. nothing"]

    for i in choices:
        print(i)
        print("")

    choice = input("Give me liberty or give me _____! - Patrick Henry ")

    if choice is "death":
        print("")
        print(congrats_message)
        print("")
        level_two()
    else:
        print("")
        print(loss_message)
        print("")
        update_lives()
        level_two()


def level_two():
    print("-----------------------")
    print("")
    print("Lives: ", lives)
    print("Welcome to level two!")
    print("")

    choices = ["1. stupidity", "2. wisdom", "3. nobody"]

    for i in choices:
        print(i)
        print("")

    choice = input("Knowledge speaks, but ______ listens. - Jimi Hendrix ")

    if choice is "wisdom":
        print("")
        print(congrats_message)
        print("")
        level_three()
    else:
        print("")
        print(loss_message)
        print("")
        update_lives()
        level_three()


def level_three():
    print("-----------------------")
    print("")
    print("Lives: ", lives)
    print("")
    print("Wow. I am impressed!")


print("")
print(welcome_message)
print("Version: " + version)

ask_view_rules()

print("")
print("We will now begin the game!")
print("")

level_one()

1 个答案:

答案 0 :(得分:0)

正如cricket_007指出,您需要在代码中使用==而不是is。这是因为如果两个变量都指向同一个对象,则is返回True,而如果两个变量的值相等,则==返回True。我还建议您在代码\n中使用换行符,而不是使用print(“”)

相关问题