![](/img/trans.png)
[英]I am having trouble with an IF statement setting a variable to True and False
[英]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.