繁体   English   中英

我无法更新变量的值

[英]I am having trouble updating the value of a variable

这是一个 python 刽子手程序。 当用户猜测错误答案时,我希望变量guesses_left 减少1。 无论猜测什么,它都停留在 9 个错误的猜测中。 我该如何解决这个问题,让它从 1 一直减少到 0? 谢谢!

#initializes the secret word and starts with empty dashes list
secret_word = "eggplant"
dashes_list = []
def get_guess(guesses_left):
    #prompts user for a guess
    guess = input("Guess: ")
    dashes = "-"
    #if the guess is not one character
    if len(guess) != 1:
        print ("Your guess must have exactly one character!")
    #if the guess is not lowercase
    elif not guess.islower():
        print ("Your guess must be a lowercase letter!")
    #assigns this position_of_letter variable to be used later
    position_of_letter = 0
    for letter in secret_word:
            if guess == letter:
                print ("Letter is in secret word.")
                update_dashes(secret_word, dashes, guess)
                return
            else:
                position_of_letter += 1
                if position_of_letter == len(secret_word):
                    print ("Letter is not in the secret word.")
                    if guesses_left==0:
                        print("You lose. The word was "+secret_word)
                        exit()
                    guesses_left=guesses_left-1
                    print (str(guesses_left)+" incorrect guesses left.")
#This goes through the word and makes sure to update
#the dashes at the right place
def update_dashes(secret_word, dashes, guess):
    position_of_letter_dashes_list = 0
    for letter in secret_word:
        if letter == guess:
            dashes_list[position_of_letter_dashes_list] = guess
        position_of_letter_dashes_list += 1

#adds a dash mark for each letter so there is now a list of dashes    
for i in range(len(secret_word)):
    dashes_list.append("-")
#The .join breaks the dashes list into a continuous string of dashes
#The "" is there so that nothing comes before each dash
guesses_left=10
while True:
    print ("".join(dashes_list))
    get_guess(guesses_left)
    if "-" not in dashes_list:
        print("Congrats! You win. The word was "+secret_word+".")
        break

目前,您将guesses_left作为参数传递给get_guess ,当您递减它时,您不会影响全局变量。 您可以通过更新以下(标记的)行返回get_guess留下的猜测数并将其存储到guesses_left中来纠正此问题:

if len(guess) != 1:
    print ("Your guess must have exactly one character!")
    return guesses_left  # THIS LINE
#if the guess is not lowercase
elif not guess.islower():
    print ("Your guess must be a lowercase letter!")
    return guesses_left  # THIS LINE
    #assigns this position_of_letter variable to be used later
position_of_letter = 0
for letter in secret_word:
    if guess == letter:
          print ("Letter is in secret word.")
          update_dashes(secret_word, dashes, guess)
          return guesses_left  # THIS LINE
    else:
          position_of_letter += 1
          if position_of_letter == len(secret_word):
               print ("Letter is not in the secret word.")
               if guesses_left==0:
                    print("You lose. The word was "+secret_word)
                    exit()
               guesses_left=guesses_left-1
               print (str(guesses_left)+" incorrect guesses left.")
               return guesses_left  # THIS LINE
...
while True:
    guesses_left = get_guess(guesses_left)  # THIS LINE
    if "-" not in dashes_list:
        print("Congrats! You win. The word was "+secret_word+".")
        break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM