我创建了一个猜谜游戏,但我不明白为什么我的-=命令功能不起作用

时间:2019-10-29 15:39:25

标签: python python-3.x function

我是一个非常新的程序员(不到一个月前开始)。我真的需要一些帮助。 抱歉,如果有点长... 这是如何工作的,是每当我发现错误时(很明显),我的猜测就会消失。还是应该这样。

这是我作为a子手项目的原型创建的程序。一旦确定正确,就可以尝试更大的项目。告诉我上面的完整命令对您来说是否有所不同,或者您对如何使其更短或更短有任何建议,或者现在尝试进行如此大的项目为时尚早。谢谢!

import random

player_name = input("What is your name? ")
print("Good luck, " + player_name + "!")

words = ["program", "giraffe", "python", "lesson", "rainbows", "unicorns", "keys", "exercise"]
guess = " "
repeat = True

word = random.choice(words)
guesses = int(len(word))

while repeat is True:
    print("The word is " + str(len(word)) + " characters long.")
    guess = input("Enter your guess: ")
    if guess != word:
        guesses -= 1
        print("Incorrect")
        print("Try again")
    elif guess == word:
        print("Good job!")
        print(str.capitalize(word) + " is the right answer!")
        repeat = input("Do you want to play again? (input Yes/No)")
        if repeat == "Yes" or "yes":
            word = random.choice(words)
            repeat = True
        elif repeat == "No" or "no":
            print("Better luck next time!")
            repeat = False
        if guesses == 1:
            print("You only have one chance left.")
        if guesses <= 0:
            print("You lose...")
            repeat = input("Do you want to play again? (input Yes/No)")
            if repeat == "Yes" or "yes":
                repeat = True
            elif repeat == "No" or "no":
                print("Better luck next time!")
                repeat = False

1 个答案:

答案 0 :(得分:0)

问题在于您的条件条款范围

while repeat is True:
    print("The word is " + str(len(word)) + " characters long.")
    guess = input("Enter your guess: ")
    if guess != word:
        guesses -= 1
        print("Incorrect")
        print("Try again")
    elif guess == word:
        print("Good job!")
        print(str.capitalize(word) + " is the right answer!")
        repeat = input("Do you want to play again? (input Yes/No)")
        if repeat == "Yes" or "yes":
            word = random.choice(words)
            repeat = True
        elif repeat == "No" or "no":
            print("Better luck next time!")
            repeat = False
    if guesses == 1:
        print("You only have one chance left.")
    if guesses <= 0:
        print("You lose...")
        repeat = input("Do you want to play again? (input Yes/No)")
        if repeat == "Yes" or "yes":
            repeat = True
        elif repeat == "No" or "no":
            print("Better luck next time!")
            repeat = False

这将解决猜测问题,但是您需要重构再次播放逻辑。 像这样

repeat = True
gameOver = False

while repeat is True:
    print("The word is " + str(len(word)) + " characters long.")
    guess = input("Enter your guess: ")
    if guess != word:
        guesses -= 1
        print("Incorrect")
        print("Try again")
    elif guess == word:
        print("Good job!")
        print(str.capitalize(word) + " is the right answer!")
        gameOver = True
    if guesses == 1:
        print("You only have one chance left.")
    if guesses <= 0:
        print("You lose...")
        gameOver = True

    if gameOver:
        playAgain = input("Do you want to play again? (input Yes/No)")
        if playAgain == "Yes" or "yes":
            word = random.choice(words)
            repeat = True
            gameOver = False
        elif repeat == "No" or "no":
            print("Better luck next time!")
            repeat = False