2016-09-20 33 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