英文:
Rock,Paper,Scissors but always player 1 wins
问题
我用Python制作了这个剪刀石头布游戏,我期望根据他们在提示期间的选择,使玩家1或玩家2获胜。但是,它总是显示玩家1获胜。在按照标准剪刀石头布规则应该让玩家2获胜的情况下,我该如何让玩家2获胜?
抱歉如果这个问题有点傻,我是个初学者。
print("欢迎来到剪刀石头布游戏!")
print("使用R代表石头,P代表纸,S代表剪刀")
print()
n = input("你想要输入名字吗?(Y/N):")
if n == "yes" or n == "Yes" or n == "Y" or n == "y":
p1n = input("玩家1的名字?:")
p2n = input("玩家2的名字?:")
name = True
else:
name = False
from getpass import getpass as input
p1=input("你选择什么?(R、P或S)(p1):")
p2=input("你选择什么?(R、P或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("玩家1赢了!GG!🤩")
if name == True:
print(p1n,"赢了!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("玩家1赢了!GG!🤩")
if name == True:
print(p1n,"赢了!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("玩家1赢了!GG!🤩")
if name == True:
print(p1n,"赢了!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("平局!哈哈 😄🤞!")
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("平局!哈哈 😄🤞!")
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("平局!哈哈 😄🤞!")
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("玩家2赢了!干得漂亮!😆")
if name == True:
print(p2n,"赢了!干得漂亮!😆")
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("玩家2赢了!干得漂亮!😆")
if name == True:
print(p2n,"赢了!干得漂亮!😆")
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("玩家2赢了!干得漂亮!😆")
if name == True:
print(p2n,"赢了!干得漂亮!😆")
else:
print("别愚弄我,傻瓜!(如果你输入的是scissor而不是scissors,那不是我的错!)")
<details>
<summary>英文:</summary>
I made this rock-paper-scissors game in Python, and I expected it to make player 1 or player 2 win according to their choice during the prompt. However, it always shows that player 1 has won. How do I make player 2 win in situations where they are supposed to win according to standard rock-paper-scissors rules?
Sorry for the silly question if this is one, I am a beginner.
```Python
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!)")
答案1
得分: 4
你的代码问题在于 if-elif
语句的逻辑。每个 if-elif
语句内的条件需要使用 括号 正确分组,否则 Python 解释器不会按照你的期望进行解释。
在 Python 中,and
运算符的优先级高于 or
运算符。这意味着你的条件会被解释成你所不希望的方式。
例如,让我们考虑以下条件:
p1 == "paper" or p1 == "Paper" or p1 == "p" or p1 == "P" and p2 == "rock" or p2 == "Rock" or p2 == "r" or p2 == "R"
这被解释为:
p1 == "paper" or p1 == "Paper" or p1 == "p" or (p1 == "P" and p2 == "rock") or p2 == "Rock" or p2 == "r" or p2 == "R"
这意味着如果 p1 等于 "P",而 p2 等于 "rock",或者如果 p1 等于 "paper"、"Paper" 或 "p",无论 p2 是什么,条件都会评估为 True。这就是为什么玩家 1 似乎总是赢的原因。
你需要使用括号来确保条件被按照你的期望进行评估:
(p1 == "paper" or p1 == "Paper" or p1 == "p" or p1 == "P") and (p2 == "rock" or p2 == "Rock" or p2 == "r" or p2 == "R")
现在,只有在玩家 1 选择 "paper" 而玩家 2 选择 "rock" 时,此条件才会评估为 True。
在你的 if-elif 语句中的其他条件也应用相同的概念来纠正游戏逻辑。
英文:
The issue with your code is in the logic of the if-elif
statements. The conditions inside each if-elif
statement need to be correctly grouped with parentheses, otherwise the Python interpreter doesn't interpret them as you might expect.
The and
operator has higher precedence than the or
operator in Python. This means that your conditions are being interpreted in a way that you didn't intend.
For instance, let's consider the condition:
p1 == "paper" or p1 == "Paper" or p1 == "p" or p1 == "P" and p2 == "rock" or p2 == "Rock" or p2 == "r" or p2 == "R"
This is being interpreted as:
p1 == "paper" or p1 == "Paper" or p1 == "p" or (p1 == "P" and p2 == "rock") or p2 == "Rock" or p2 == "r" or p2 == "R"
which means if p1 equals "P", and p2 equals "rock", or if p1 equals "paper", "Paper", or "p", regardless of what p2 is, the condition will evaluate to True. This is why Player 1 seems to always win.
You need to use parentheses to ensure that the condition is evaluated as you expect:
(p1 == "paper" or p1 == "Paper" or p1 == "p" or p1 == "P") and (p2 == "rock" or p2 == "Rock" or p2 == "r" or p2 == "R")
Now this condition will only evaluate to True if Player 1 selects paper and Player 2 selects rock.
Apply the same concept to the other conditions in your if-elif statements to correct the game logic.
答案2
得分: 1
使您的生活更轻松,完全避免与多个值进行错误比较:
beats = {"r": "s", "s": "p", "p": "r"}
# ...
n = input("您想要姓名吗? (Y/N):")
if n.lower()[0] == "y":
p1n = input("玩家1的姓名?:")
p2n = input("玩家2的姓名?:")
else:
p1n = "玩家1"
p2n = "玩家2"
# ...
p1 = p1.lower()[0]
p2 = p2.lower()[0]
if p1 == p2:
print("平局!哈哈 😂🤣!")
elif beats.get(p1) == p2:
print(f"{p1n} 赢了!玩得不错! 😊👏")
elif beats.get(p2) == p1:
print(f"{p2n} 赢了!玩得不错! 😊👏")
else:
# 提供无意义输入的随机侮辱
英文:
Make your life easier and avoid buggy comparisons against multiple values altogether:
beats = {"r": "s", "s": "p", "p": "r"}
# ...
n = input("Do you want name? (Y/N) :")
if n.lower()[0] == "y":
p1n = input("Name for player 1? :")
p2n = input("Name for player 2? :")
else:
p1n = "Player 1"
p2n = "Player 2"
# ...
p1 = p1.lower()[0]
p2 = p2.lower()[0]
if p1 == p2:
print("Its a tie! LOL 💀🤣!")
elif beats.get(p1) == p2:
print(f"{p1n} has won! Well Played!😎")
elif beats.get(p2) == p1:
print(f"{p2n} has won! Well Played!😎")
else:
# random insult for providing nonsensical input
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论