2016-09-20 46 views
0

你好,我是新来编码,但我一直坚持代码很长,似乎无法找到答案这里在stackoverflow。Python 3.X岩石剪刀Lizard Spock问题

无论我做什么,答案都以玩家1的第一张照片胜出:玩家一胜,摇滚跳动剪刀。

player1 = input("Player One do you want Rock, Paper, Scissors, Lizard or Spock?") 
player2 = input("Player Two do you want Rock, Paper, Scissors, Lizard or Spock?") 
print(player1) 
print(player2) 

rock = 1 
paper = 2 
scissors = 3 
lizard = 4 
spock = 5 

#Tie 

if (player1 == player2): 
print("It's a tie.") 

#Player 1 wins 

elif (player1 == 1, player2 == 3): 
    print("Player One wins, Rock beats Scissors.") 
elif (player1 == 1, player2 == 4): 
    print("Player One wins, Rock beats Lizard.") 
elif (player1 == 2, player2 == 1): 
    print("Player One wins, Paper beats Rock.") 
elif (player1 == 2, player2 == 5): 
    print("Player One wins, Paper beats Spock.") 
elif (player1 == 3, player2 == 2): 
    print("Player One wins, Scissors beats Paper.") 
elif (player1 == 3, player2 == 4): 
    print("Player One wins, Scissors beats Lizard.") 
elif (player1 == 4, player2 == 2): 
    print("Player One wins, Lizard beats Paper.") 
elif (player1 == 4, player2 == 5): 
    print("Player One wins, Lizard beats Spock.") 
elif (player1 == 5, player2 == 3): 
    print("Player One wins, Spock beats Scissors.") 
elif (player1 == 5 , player2 == 1): 
    print("Player One wins, Spock beats Rock.") 

#Player 2 wins  

elif (player2 == 1, player1 == 3): 
    print("Player Two wins, Rock beats Scissors.") 
elif (player2 == 1, player1 == 4): 
    print("Player Two wins, Rock beats Lizard.") 
elif (player2 == 2, player1 == 1): 
    print("Player Two wins, Paper beats Rock.") 
elif (player2 == 2, player1 == 5): 
    print("Player Two wins, Paper beats Spock.") 
elif (player2 == 3, player1 == 2): 
    print("Player Two wins, Scissors beats Paper.") 
elif (player2 == 3, player1 == 4): 
    print("Player Two wins, Scissors beats Lizard.") 
elif (player2 == 4, player1 == 2): 
    print("Player Two wins, Lizard beats Paper.") 
elif (player2 == 4, player1 == 5): 
    print("Player Two wins, Lizard beats Spock.") 
elif (player2 == 5, player1 == 3): 
    print("Player Two wins, Spock beats Scissors.") 
elif (player2 == 5 , player1 == 1): 
    print("Player Two wins, Spock beats Rock.") 

回答

6

您没有正确构建自己的条件。

elif (player1 == 1, player2 == 3) 

这将创建一个tuple,然后检查其是否有感实性,它总是成功,因为这tuple不为空。您需要使用逻辑运算符and

elif player1 == 1 and player2 == 3 

这将检查这两方面的条件是否为真。对代码中的所有类似实例执行此操作。

此外,目前还不清楚你是从这里用户期待什么:

player1 = input("Player One do you want Rock, Paper, Scissors, Lizard or Spock?") 
player2 = input("Player Two do you want Rock, Paper, Scissors, Lizard or Spock?") 
print(player1) 
print(player2) 

rock = 1 
paper = 2 
scissors = 3 
lizard = 4 
spock = 5 

它看起来像假设用户输入类似Rock,然后你想到rock = 1线'Rock'转换为1。它不这样工作。这样做的最基本的方法是与另一个if..elif块,但一本字典会更好:

player1 = input("Player One do you want Rock, Paper, Scissors, Lizard or Spock?") 
player2 = input("Player Two do you want Rock, Paper, Scissors, Lizard or Spock?") 
print(player1) 
print(player2) 

d = {'Rock':1, 'Paper':2, 'Scissors':3, 'Lizard':4, 'Spock':5} 

player1 = d.get(player1) 
player2 = d.get(player2) 
+0

你的解决方案中的冒号缺失 - 它应该是'elif player1 == 1和player2 == 3:'这可能会混淆初学者,就像@Omar说的那样。 – Maurice

+0

@Maurice - 我没有引用完整的一行。 – TigerhawkT3

+0

你好,谢谢你的回答,我不知道你可以用'和'和'd'。不幸的是,现在当我尝试做任何组合时,除了用户输入(例如1,2,3,4)之外绝对没有任何组合。 – Omar

0

虽然TigerHawk覆盖你的condiction,您还可以投你输入整数。

player1 = int(player1) 
player2 = int(player2) 

现在你是一个STR(你的输入)比较为int(player == 1)。这不会产生你想要的。

player1 == 1  #fails right now since it's like asking "1" == 1 which fails. 
int(player1) == 1 # passes since it's asking 1 == 1. 

此外,您的print("It's a tie.")是缩进错误。

+0

感谢您的帮助,即使当我这样做时,我也遇到了同样的问题TigerHawk他的解决方案:我得到的唯一输出是像1,2,3,4这样的用户输入,除非我执行Tie解决方案。 – Omar

+0

也是你的'print(“这是一条平行线。”)'是错误的。 @Omar你可能想在你的问题中编辑你的代码。 – MooingRawr

+0

我只是做了修复,但正如我上面提到的那样,问题仍然存在。 – Omar