[英]How to debug the loop in my Python hangman game
我正在嘗試創建一個劊子手游戲,但有一個問題。 每當我運行我的代碼時, random.choice(words_to_guess)
選擇一個詞,當我寫下它選擇的詞並且它是正確的詞時,它不會停止。 它一直在循環。
我該怎么做才能停止循環?
import random
lives = 10
def welcome():
name = input("enter your name: ")
print("hello " + name)
print( name + " you will have this many(10) ")
def computer_role():
words_to_guess = ["salad", "mood"]
random.choice(words_to_guess)
return random.choice(words_to_guess)
computer = computer_role()
def game_correct():
user = input("enter a letter: ")
while user in computer:
print(user)
game_correct()
def game_wrong():
for user in computer_role():
cal = lives -1
return cal
print("wrong word" + user + "this is how many lives you have left" + str(lives))
welcome()
computer_role()
game_correct()
game_wrong()
我曾嘗試在game_correct()
所在的位置下使用break
,但似乎也無法正常工作。 我也試過exit()
但這也不起作用。
如果您猜對了一個字母,while 循環將永遠運行,因為它永遠不會更改名為 user 的變量。 您應該使用 if,而不是使用 while 循環。
def game_correct():
user = input("enter a letter: ")
if user in computer:
print("Correct guess")
應該指出你正確的方向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.