首页 文章

岩石,纸,剪刀,Spock,蜥蜴在python中,玩家2自动获胜

提问于
浏览
0

对于练习,我们需要重新创建bigbang理论成员所玩的游戏: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!")

1 回答

  • 3

    你的每个if语句都有一个else . 只有一个if语句可以为true,因此这意味着将评估所有其他语句 . 结果是最后一个else语句 - 将s设置为False - 将“赢”,因此玩家2获胜 .

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

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

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

相关问题