簡體   English   中英

我創建了一個猜謎游戲,但我不明白為什么我的 -= 命令 function 不起作用

[英]I created a guessing game and I can't understand why my -= command function doesn't work

我是一個非常新的程序員(不到一個月前開始)。 我真的需要一些幫助。 對不起,如果它有點長......這是如何工作的,我每次出錯時都會猜測 go (很明顯)。 或者它應該,無論如何。

這是我作為劊子手項目原型創建的程序。 一旦我做對了,我就可以嘗試更大的項目。 告訴我上面的完整命令對你的工作方式是否不同,或者你對如何使它更短或更好有任何建議,或者我現在嘗試這樣大的項目還為時過早。 謝謝!

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

問題在於您的條件句的 scope

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM