石头,剪刀,布,但总是玩家1赢。

huangapple go评论77阅读模式
英文:

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(&quot;Welcome to Rock, Paper, Scissors!&quot;)
print(&quot;Use R for Rock , P for Paper and S for Scissors&quot;)
print()
n = input(&quot;Do you want name? (Y/N) :&quot;)
if n == &quot;yes&quot; or n == &quot;Yes&quot; or n == &quot;Y&quot; or n == &quot;y&quot;:
  p1n = input(&quot;Name for player 1? :&quot;)
  p2n = input(&quot;Name for player 2? :&quot;)
  name = True
else:
  name = False
from getpass import getpass as input
p1=input(&quot;What do you choose?(R,P or S)(p1): &quot;)
p2=input(&quot;What do you choose?(R,P or S)(p2): &quot;)
if p1 == &quot;paper&quot; or p1 == &quot;Paper&quot; or p1 == &quot;p&quot; or p1 == &quot;P&quot; and p2 == &quot;rock&quot; or p2 == &quot;Rock&quot; or p2 == &quot;r&quot; or p2 == &quot;R&quot;:
  print(&quot;Player 1 has won! GG!&#128520;&quot;)
  if name == True:
    print(p1n,&quot;has won! GG!&#128520;&quot;)
elif p1 == &quot;rock&quot; or p1 == &quot;Rock&quot; or p1 == &quot;r&quot; or p1 == &quot;R&quot; and p2 == &quot;scissors&quot; or p2 == &quot;Scissors&quot; or p2 == &quot;s&quot; or p2 == &quot;S&quot;:
  print(&quot;Player 1 has won! GG!&#128520;&quot;)
  if name == True:
    print(p1n,&quot;has won! GG!&#128520;&quot;)
elif p1 == &quot;scissors&quot; or p1 == &quot;Scissors&quot; or p1 == &quot;s&quot; or p1 == &quot;S&quot; and p2 == &quot;paper&quot; or p2 == &quot;Paper&quot; or p2 == &quot;p&quot; or p2 == &quot;P&quot;:
  print(&quot;Player 1 has won! GG!&#128520;&quot;)
  if name == True:
    print(p1n,&quot;has won! GG!&#128520;&quot;)
elif p1 == &quot;rock&quot; or p1 == &quot;Rock&quot; or p1 == &quot;r&quot; or p1 == &quot;R&quot; and p2 == &quot;rock&quot; or p2 == &quot;Rock&quot; or p2 == &quot;r&quot; or p2 == &quot;R&quot;:
  print(&quot;Its a tie! LOL &#128128;&#129315;!&quot;)
elif p1 == &quot;paper&quot; or p1 == &quot;Paper&quot; or p1 == &quot;p&quot; or p1 == &quot;P&quot; and p2 == &quot;paper&quot; or p2 == &quot;Paper&quot; or p2 == &quot;p&quot; or p2 == &quot;P&quot;:
  print(&quot;Its a tie! LOL &#128128;&#129315;!&quot;)
elif p1 == &quot;scissors&quot; or p1 == &quot;Scissors&quot; or p1 == &quot;s&quot; or p1 == &quot;S&quot; and p2 == &quot;scissors&quot; or p2 == &quot;Scissors&quot; or p2 == &quot;s&quot; or p2 == &quot;S&quot;:
  print(&quot;Its a tie! LOL &#128128;&#129315;!&quot;)
elif p2 == &quot;rock&quot; or p2 == &quot;Rock&quot; or p2 == &quot;r&quot; or p2 == &quot;R&quot; and p1 == &quot;scissors&quot; or p1 == &quot;Scissors&quot; or p1 == &quot;s&quot; or p1 == &quot;S&quot;:
  print(&quot;Player 2 has won! Well Played!&#128526;&quot;)
  if name == True:
    print(p2n,&quot;has won! Well Played!&#128526;&quot;)
elif p2 == &quot;paper&quot; or p2 == &quot;Paper&quot; or p2 == &quot;p&quot; or p2 == &quot;P&quot; and p1 == &quot;rock&quot; or p1 == &quot;Rock&quot; or p1 == &quot;r&quot; or p1 == &quot;R&quot;:
  print(&quot;Player 2 has won! Well Played!&#128526;&quot;)
  if name == True:
    print(p2n,&quot;has won! Well Played!&#128526;&quot;)
elif p2 == &quot;scissors&quot; or p2 == &quot;Scissors&quot; or p2 == &quot;s&quot; or p2 == &quot;S&quot; and p1 == &quot;paper&quot; or p1 == &quot;Paper&quot; or p1 == &quot;p&quot; or p1 == &quot;P&quot;:
  print(&quot;Player 2 has won! Well Played!&#128526;&quot;)
  if name == True:
    print(p2n,&quot;has won! Well Played!&#128526;&quot;)
else:
  print(&quot;Don&#39;t trick me dumbo!(And if u put scissor intead of scissors then its not my fault!)&quot;)

答案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 == &quot;paper&quot; or p1 == &quot;Paper&quot; or p1 == &quot;p&quot; or p1 == &quot;P&quot; and p2 == &quot;rock&quot; or p2 == &quot;Rock&quot; or p2 == &quot;r&quot; or p2 == &quot;R&quot;

This is being interpreted as:

p1 == &quot;paper&quot; or p1 == &quot;Paper&quot; or p1 == &quot;p&quot; or (p1 == &quot;P&quot; and p2 == &quot;rock&quot;) or p2 == &quot;Rock&quot; or p2 == &quot;r&quot; or p2 == &quot;R&quot;

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 == &quot;paper&quot; or p1 == &quot;Paper&quot; or p1 == &quot;p&quot; or p1 == &quot;P&quot;) and (p2 == &quot;rock&quot; or p2 == &quot;Rock&quot; or p2 == &quot;r&quot; or p2 == &quot;R&quot;)

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 = {&quot;r&quot;: &quot;s&quot;, &quot;s&quot;: &quot;p&quot;, &quot;p&quot;: &quot;r&quot;}
# ...
n = input(&quot;Do you want name? (Y/N) :&quot;)
if n.lower()[0] == &quot;y&quot;:
p1n = input(&quot;Name for player 1? :&quot;)
p2n = input(&quot;Name for player 2? :&quot;)
else:
p1n = &quot;Player 1&quot;
p2n = &quot;Player 2&quot;
# ...
p1 = p1.lower()[0]
p2 = p2.lower()[0]
if p1 == p2:
print(&quot;Its a tie! LOL &#128128;&#129315;!&quot;)
elif beats.get(p1) == p2:
print(f&quot;{p1n} has won! Well Played!&#128526;&quot;)
elif beats.get(p2) == p1:
print(f&quot;{p2n} has won! Well Played!&#128526;&quot;)
else:
# random insult for providing nonsensical input

huangapple
  • 本文由 发表于 2023年6月19日 18:23:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定