【问题标题】:Rock, Paper, Scissor, Spock, Lizard in python, Player 2 automatically winsPython 中的石头、剪子、剪刀、斯波克、蜥蜴,玩家 2 自动获胜
【发布时间】:2015-09-28 08:21:21
【问题描述】:

对于一个练习,我们需要重现大爆炸理论成员所玩的游戏:Rock、Paper、Scissor、Spock、Lizard。我设法几乎完全重新创建它,唯一的问题是:玩家 2 自动获胜。有人可以告诉我需要在哪里更改代码并解释原因吗?

import sys

t = len(sys.argv)

if(t < 2 or t > 3):
    print("Usage: rpsls.py symbool1 symbool2")
    exit()
i = 1
while (i > 0):
    a = sys.argv[1]
    b = sys.argv[2]
    a = a.lower()
    b = b.lower()
    if(a != "rock" and a != "paper" and a != "scissor" and a != "lizard" and a != "spock"):
        print("What's that? please use a real symbol!")

    elif(b != "rock" and b != "paper" and b != "scissor" and b != "lizard" and b != "spock"):
        print("What's that? please use a real symbol!")

    else:
        if (a == "paper" and b == "scissor"):
            s = True
            i = 0
        else:
            s = False
            i = 0
        if(a == "paper" and b == "rock"):
            s = True
            i = 0
        else:
            s = False
            i = 0
        if(a == "rock" and b == "lizard"):
            s = True
            i = 0
        else:
            s = False
            i = 0
        if(a == "lizard" and b == "spock"):
            s = True
            i = 0
        else:
            s = False
            i = 0
        if(a == "spock" and b == "scissors"):
            s = True
            i = 0
        else:
            s = False
            i = 0
        if(a == "scissor" and b == "lizard"):
            s = True
            i = 0
        else:
            s = False
            i = 0
        if(a == "lizard" and b == "paper"):
            s = True
            i = 0
        else:
            s = False
            i = 0
        if(a == "paper" and b == "spock"):
            s = True
            i = 0
        else:
            s = False
            i = 0
        if(a == "spock" and b == "rock"):
            s = True
            i = 0
        else:
            s = False
            i = 0
        if(a == "rock" and b == "scissor"):
            s = True
            i = 0
        else:
            s = False
            i = 0
        if(a == b):
            print("It's a tie!")
            i = 0
            exit()

if(s == True):
        print("Player 1 wins!")
if(s == False):
        print("Player 2 wins!")

【问题讨论】:

  • 你可以忽略所有的 if 语句,除了倒数第二个,所以如果 a 是石头,b 是剪刀,它可能会让玩家赢
  • 我怎样才能让它忽略所有其他的?
  • 您已经是,您在倒数第二个 if 语句中重新分配 s 和 i 的值,还请注意您对 scissors的条件检查之一
  • 就像你可以写的一般提示 - 一个不在 ["rock", "paper",....]

标签: python loops if-statement


【解决方案1】:

您的每个 if 语句都有一个 else。只有一个 if 语句可以为真,这意味着所有其他 else 语句都被评估。结果是最后一个 else 语句(将 s 设置为 False)将“获胜”,因此玩家 2 获胜。

您应该删除所有 else 语句,并将代码重组为一系列 if...elif... 块:

   if a == "paper" and b == "scissor":
        s = True
        i = 0
   elif a == "paper" and b == "rock":

(注意,如果条件不需要括号。)

【讨论】:

  • 我最好也嵌套 if's 这样操作就不必继续评估 a 是否等于纸 :)
  • 那么,将所有 if 改为 elif,最后改为 else?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多