Python石头剪刀布游戏添加重启功能后故障,请求代码错误排查
石头剪刀布游戏添加重启功能异常排查
我是编程新手,之前的Python石头剪刀布游戏运行正常,但尝试添加按'r'键重启游戏的功能后程序出现异常。以下是未添加重启功能时可正常运行的代码:
import random options = ('Rock','Paper','Scissors') player_score = 0 computer_score = 0 def game(player, computer): global player_score global computer_score if player == computer: print(f"Both You and the Computer chose the same option {player}, It's a tie!") elif player == 'Rock': if computer == 'Paper': player_score += 1 print(f'Paper covers the rock, you win 1 point, your score is {player_score}.') elif computer == 'Scissors': player_score += 1 print(f'Rock smashes the scissors, you win 1 point, your score is {player_score}.') elif player == 'Scissors': if computer == 'Rock': computer_score += 1 print(f'Rock smaches the scissors, computer wins 1 point, your score is {player_score}') elif computer == 'Paper': player_score += 1 print(f'Scissors cut the paper, you win 1 point, your score is {player_score}.') elif player == 'Paper': if computer == 'Rock': player_score += 1 print(f'Paper covers the rock, you win 1 point, your score is {player_score}.') elif computer == 'Scissors': computer_score += 1 print(f'Scissors cut the paper, computer wins 1 point, your score is {player_score}.') while True: if player_score == 10: print('*' * 30, '\nYour Score reached 10, You Win!', '*'*30) exit() elif computer_score == 10: print('*' * 30, '\nComputer Score reached 10, You Lose!', '*'*30) exit() player_choice = input('Please choose Rock, Paper, Scissor or Q for quit: ').lower() computer_choice = random.choice(options) if player_choice == 'quit': print('FINISH!') exit() elif not player_choice in options: print('Wrong Option!, please try again') else: game(player_choice, computer_choice)
常见错误点分析
添加'r'重启功能时,容易出现以下问题导致程序异常:
- 分数未重置:仅重启循环但未将
player_score和computer_score归零,新旧分数叠加后,游戏终止条件逻辑完全混乱。 - 输入判断顺序错误:把'r'的判断放在有效选项检查之后,导致输入'r'被判定为错误选项。
- 原代码隐藏bug:输入转为小写后,与大写的
options元组匹配失败(比如输入'rock'会被当成错误选项),这也是添加功能后异常的潜在诱因。
修正后的完整代码
import random # 改为小写,匹配输入的小写格式 options = ('rock', 'paper', 'scissors') player_score = 0 computer_score = 0 def game(player, computer): global player_score global computer_score if player == computer: print(f"你和电脑都选了{player},平局!") elif player == 'rock': if computer == 'paper': computer_score += 1 print(f'纸包住石头,电脑得1分,你的分数是{player_score}。') elif computer == 'scissors': player_score += 1 print(f'石头砸碎剪刀,你得1分,你的分数是{player_score}。') elif player == 'scissors': if computer == 'rock': computer_score += 1 print(f'石头砸碎剪刀,电脑得1分,你的分数是{player_score}。') elif computer == 'paper': player_score += 1 print(f'剪刀剪碎纸,你得1分,你的分数是{player_score}。') elif player == 'paper': if computer == 'rock': player_score += 1 print(f'纸包住石头,你得1分,你的分数是{player_score}。') elif computer == 'scissors': computer_score += 1 print(f'剪刀剪碎纸,电脑得1分,你的分数是{player_score}。') while True: if player_score == 10: print('*' * 30, '\n你的分数达到10分,你赢了!', '*'*30) break elif computer_score == 10: print('*' * 30, '\n电脑分数达到10分,你输了!', '*'*30) break player_choice = input('请选择 rock、paper、scissors,按q退出,按r重启游戏:').lower() # 优先处理重启和退出逻辑 if player_choice in ('q', 'quit'): print('游戏结束!') break if player_choice == 'r': player_score = 0 computer_score = 0 print('游戏已重启,分数已清零!') continue # 跳过后续逻辑,直接进入下一轮循环 # 检查输入有效性 if player_choice not in options: print('无效选项,请重新输入!') continue computer_choice = random.choice(options) game(player_choice, computer_choice)
修正说明
- 将
options改为小写,与输入的小写格式统一,解决原代码的匹配bug。 - 把'r'重启的判断放在有效选项检查之前,确保输入'r'能被正确识别。
- 重启时重置分数为0,并使用
continue跳过后续逻辑,直接进入下一轮循环。 - 将
exit()改为break,更优雅地终止循环,避免强制退出程序。 - 提示文本改为中文,提升操作友好度。
内容的提问来源于stack exchange,提问作者remoo heshmat
相关产品推荐
相关产品推荐

