如何修复Python猜数字游戏中的while循环条件问题
猜数字游戏循环终止条件修复问题
问题描述
我开发了一款猜数字游戏,规则为:每轮生成新的秘密数字,玩家先猜,随后电脑猜,玩家或电脑率先获得2分即获胜。我原本用while循环嵌套for循环执行100次game()函数,循环条件设置为user_score != 2 or computer_score != 2,但运行时循环会直接执行满100次,完全忽略终止条件,请问该如何修复这个while循环条件问题?
相关代码片段
while user_score != 2 or computer_score != 2: for _ in range(100): game()
完整代码
import random computer_score = 0 user_score = 0 def game(): global user_score global computer_score secret_number = random.randrange(10) while True: user_guess = input("Guess a number from 0 - 10: ") if user_guess.isdigit(): user_guess = int(user_guess) if 0 <= user_guess <= 10: break else: print("Enter a valid number from 0 - 10") else: print("Please enter a valid number from 0 - 10") computer_guess = random.randrange(10) if user_guess == secret_number: print("Hooray! You guessed correctly") user_score += 1 print(f"Your score is {user_score}") else: print("Sorry wrong guess") print("Now Player 2's turn") if computer_guess == secret_number: print("Player 2 guessed the correct number!") computer_score += 1 print(f"Player 2's score is {computer_score}") else: print("Player 2 guesed incorrectly") print( f" Your score: {user_score}" + " " + f"Player 2 score: {computer_score}") game() while user_score != 2 or computer_score != 2: for _ in range(100): game() if user_score == 2: print(f"Hooray! You won with a score of {user_score} points") print(f"Player 2 got {computer_score} points") elif computer_score == 2: print(f"Sorry, you lost. Your score is {user_score}, and Player 2 got {computer_score} points") print( f" Your final score: {user_score}" + " " + f"Player 2 final score: {computer_score}" )
问题根源
- 嵌套循环逻辑冗余:
for range(100)会强制执行满100次game(),即便中途有人达到2分,也不会提前终止,要等for循环跑完才会回到while循环判断条件。 - while条件逻辑错误:
user_score != 2 or computer_score != 2这个条件永远为真(除非双方同时达到2分,几乎不可能)。正确的循环继续条件应该是双方都未达到2分,也就是user_score < 2 and computer_score < 2。
修复方案
直接移除多余的for循环,用while循环控制游戏轮次,每次循环执行一次game(),执行后自动检查终止条件:
修改后的核心循环代码
while user_score < 2 and computer_score < 2: game()
完整修复后的代码
import random computer_score = 0 user_score = 0 def game(): global user_score global computer_score secret_number = random.randrange(10) while True: user_guess = input("Guess a number from 0 - 10: ") if user_guess.isdigit(): user_guess = int(user_guess) if 0 <= user_guess <= 10: break else: print("Enter a valid number from 0 - 10") else: print("Please enter a valid number from 0 - 10") computer_guess = random.randrange(10) if user_guess == secret_number: print("Hooray! You guessed correctly") user_score += 1 print(f"Your score is {user_score}") else: print("Sorry wrong guess") print("Now Player 2's turn") if computer_guess == secret_number: print("Player 2 guessed the correct number!") computer_score += 1 print(f"Player 2's score is {computer_score}") else: print("Player 2 guessed incorrectly") print(f" Your score: {user_score} Player 2 score: {computer_score}") # 执行第一轮游戏 game() # 循环直到任意一方达到2分 while user_score < 2 and computer_score < 2: game() if user_score == 2: print(f"Hooray! You won with a score of {user_score} points") print(f"Player 2 got {computer_score} points") elif computer_score == 2: print(f"Sorry, you lost. Your score is {user_score}, and Player 2 got {computer_score} points") print(f" Your final score: {user_score} Player 2 final score: {computer_score}" )
额外优化建议
- 避免全局变量:可以将
user_score和computer_score作为参数传入game(),或让函数返回本轮得分变化,提升代码封装性。 - 修正拼写错误:原代码中
guesed应改为guessed。
内容的提问来源于stack exchange,提问作者Aryan Singh
相关产品推荐
相关产品推荐

