Python石头剪刀布游戏异常:玩家1始终获胜的修复咨询
石头剪刀布游戏判定逻辑修复方案
问题描述
我用Python写了一款石头剪刀布游戏,原本想根据玩家选择判定胜负,但实际运行时总是显示玩家1获胜。请问怎么让游戏按规则在玩家2该赢的时候判定胜利?
原代码:
print("Welcome to Rock, Paper, Scissors!") print("Use R for Rock , P for Paper and S for Scissors") print() n = input("Do you want name? (Y/N) :") if n == "yes" or n == "Yes" or n == "Y" or n == "y": p1n = input("Name for player 1? :") p2n = input("Name for player 2? :") name = True else: name = False from getpass import getpass as input p1=input("What do you choose?(R,P or S)(p1): ") p2=input("What do you choose?(R,P or S)(p2): ") if p1 == "paper" or p1 == "Paper" or p1 == "p" or p1 == "P" and p2 == "rock" or p2 == "Rock" or p2 == "r" or p2 == "R": print("Player 1 has won! GG!😈") if name == True: print(p1n,"has won! GG!😈") elif p1 == "rock" or p1 == "Rock" or p1 == "r" or p1 == "R" and p2 == "scissors" or p2 == "Scissors" or p2 == "s" or p2 == "S": print("Player 1 has won! GG!😈") if name == True: print(p1n,"has won! GG!😈") elif p1 == "scissors" or p1 == "Scissors" or p1 == "s" or p1 == "S" and p2 == "paper" or p2 == "Paper" or p2 == "p" or p2 == "P": print("Player 1 has won! GG!😈") if name == True: print(p1n,"has won! GG!😈") elif p1 == "rock" or p1 == "Rock" or p1 == "r" or p1 == "R" and p2 == "rock" or p2 == "Rock" or p2 == "r" or p2 == "R": print("Its a tie! LOL 💀🤣!") elif p1 == "paper" or p1 == "Paper" or p1 == "p" or p1 == "P" and p2 == "paper" or p2 == "Paper" or p2 == "p" or p2 == "P": print("Its a tie! LOL 💀🤣!") elif p1 == "scissors" or p1 == "Scissors" or p1 == "s" or p1 == "S" and p2 == "scissors" or p2 == "Scissors" or p2 == "s" or p2 == "S": print("Its a tie! LOL 💀🤣!") elif p2 == "rock" or p2 == "Rock" or p2 == "r" or p2 == "R" and p1 == "scissors" or p1 == "Scissors" or p1 == "s" or p1 == "S": print("Player 2 has won! Well Played!😎") if name == True: print(p2n,"has won! Well Played!😎") elif p2 == "paper" or p2 == "Paper" or p2 == "p" or p2 == "P" and p1 == "rock" or p1 == "Rock" or p1 == "r" or p1 == "R": print("Player 2 has won! Well Played!😎") if name == True: print(p2n,"has won! Well Played!😎") elif p2 == "scissors" or p2 == "Scissors" or p2 == "s" or p2 == "S" and p1 == "paper" or p1 == "Paper" or p1 == "p" or p1 == "P": print("Player 2 has won! Well Played!😎") if name == True: print(p2n,"has won! Well Played!😎") else: print("Don't trick me dumbo!(And if u put scissor intead of scissors then its not my fault!)")
问题根源
核心问题是逻辑运算符优先级错误:Python中and的优先级高于or,导致你的条件判断完全不符合预期。比如第一个if语句:
if p1 == "paper" or p1 == "Paper" or p1 == "p" or p1 == "P" and p2 == "rock" ...
实际执行逻辑是:只要p1是"paper"/"Paper"/"p"/"P",不管p2是什么,条件都会成立,直接判定玩家1获胜。因为p1 == "P" and p2 == "rock"是一个整体,但前面的or只要有一个为真,整个条件就为真。
修复方案
1. 给条件分组加括号
把每个玩家的选择条件用括号括起来,确保逻辑正确。比如:
if (p1 == "paper" or p1 == "Paper" or p1 == "p" or p1 == "P") and (p2 == "rock" or p2 == "Rock" or p2 == "r" or p2 == "R"):
这样只有当p1是纸且p2是石头时,才触发玩家1获胜的条件。
2. 优化输入处理(可选但推荐)
把输入统一转成小写(或大写),减少重复的判断语句,让代码更简洁。比如:
p1 = input("What do you choose?(R,P or S)(p1): ").lower() p2 = input("What do you choose?(R,P or S)(p2): ").lower()
之后只需要判断p1 == 'p'或者p1 == 'paper'即可,不用再区分大小写。
修改后的完整代码
print("Welcome to Rock, Paper, Scissors!") print("Use R for Rock , P for Paper and S for Scissors") print() n = input("Do you want name? (Y/N) :").lower() name = False p1n = p2n = "" if n in ["yes", "y"]: p1n = input("Name for player 1? :") p2n = input("Name for player 2? :") name = True from getpass import getpass as input # 统一转小写,简化判断 p1 = input("What do you choose?(R,P or S)(p1): ").lower() p2 = input("What do you choose?(R,P or S)(p2): ").lower() # 玩家1获胜的条件,分组加括号 if (p1 == "paper" or p1 == "p") and (p2 == "rock" or p2 == "r"): print("Player 1 has won! GG!😈") if name: print(f"{p1n} has won! GG!😈") elif (p1 == "rock" or p1 == "r") and (p2 == "scissors" or p2 == "s"): print("Player 1 has won! GG!😈") if name: print(f"{p1n} has won! GG!😈") elif (p1 == "scissors" or p1 == "s") and (p2 == "paper" or p2 == "p"): print("Player 1 has won! GG!😈") if name: print(f"{p1n} has won! GG!😈") # 平局条件 elif p1 == p2 or (p1 in ["rock", "r"] and p2 in ["rock", "r"]) or (p1 in ["paper", "p"] and p2 in ["paper", "p"]) or (p1 in ["scissors", "s"] and p2 in ["scissors", "s"]): print("Its a tie! LOL 💀🤣!") # 玩家2获胜的条件 elif (p2 == "paper" or p2 == "p") and (p1 == "rock" or p1 == "r"): print("Player 2 has won! Well Played!😎") if name: print(f"{p2n} has won! Well Played!😎") elif (p2 == "rock" or p2 == "r") and (p1 == "scissors" or p1 == "s"): print("Player 2 has won! Well Played!😎") if name: print(f"{p2n} has won! Well Played!😎") elif (p2 == "scissors" or p2 == "s") and (p1 == "paper" or p1 == "p"): print("Player 2 has won! Well Played!😎") if name: print(f"{p2n} has won! Well Played!😎") else: print("Don't trick me dumbo!(And if u put scissor instead of scissors then its not my fault!)")
关键说明
- 所有玩家选择的判断都用括号明确分组,确保
and和or的逻辑顺序正确。 - 输入转小写后,只需要判断小写的字符和单词,减少冗余代码。
- 平局条件简化为直接判断p1和p2是否匹配,或者对应选项是否一致,更简洁。
内容的提问来源于stack exchange,提问作者ToxicGamer329
相关产品推荐
相关产品推荐

