我正在尝试创建一个猜测游戏,但是当我运行代码时,什么也没发生

时间:2020-06-25 01:54:34

标签: python python-3.x

我目前是编程的新手,所以我尝试创建一种猜谜游戏作为练习,但是我不知道自己做错了什么。

secret_word = "Giraffe" or "giraffe"
guess_word = ""
failed = False
guess_count_limit = 3
guess_count = 0

while guess_word != secret_word and not failed:
    if 1 < guess_count:
        guess = input("Guess the animal I'm thinking of: ")
        guess_count += 1
    elif 2 < guess_count:
        print("Incorrect but here's a hint: ")
        print("It's common in Africa. ")
        print("Two guesses left.")
        guess = input("Now try again: ")
        guess_count += 1
    elif 3 < guess_count:
        print("Incorrect but here's your last hint: ")
        print("It's tall. ")
        print("One guess left")
        guess = input("Now try again: ")
    elif guess_count == guess_count_limit:
        print("Nope, out of guesses.")
        failed = True

if failed:
    print("You win")
else:
    print("Wow, you really lost huh.")

当我尝试运行该程序时,什么也没发生。

1 个答案:

答案 0 :(得分:0)

此版本将起作用:

secret_word = "giraffe"
guess_word = ""
guess_count_limit = 3
guess_count = 0

while guess_word.lower() != secret_word and guess_count < guess_count_limit :
    if 1 > guess_count:
        guess_word = input("Guess the animal I'm thinking of: ")
        guess_count += 1
    elif 2 > guess_count:
        print("Incorrect but here's a hint: ")
        print("It's common in Africa. ")
        print("Two guesses left.")
        guess_word = input("Now try again: ")
        guess_count += 1
    elif 3 > guess_count:
        print("Incorrect but here's your last hint: ")
        print("It's tall. ")
        print("One guess left")
        guess_word = input("Now try again: ")
    elif guess_count == guess_count_limit:
        print("Nope, out of guesses.")
        print("Wow, you really lost huh.")

if guess_word.lower() == secret_word :
    print("You win")

您犯了3个错误:

secret_word = "Giraffe" or "giraffe"
  1. 据我所知,逻辑运算符不适用于字符串。但是,您不需要两个字母大小写不同的单词,只需要一个单词,然后更改猜测的大小写即可进行检查。

  2. 当您接受存储在猜测中的输入时,但是在循环中,您正在将guess_word与secret_word进行比较!

  3. 您在条件语句中的循环内遇到了问题,请尝试解决它以便学习并变得更好。

祝你好运!