2015-09-28 59 views
0

的鍛鍊,我們需要重新創建bigbang理論成員所玩的遊戲:Rock,Paper,Scissor,Spock,Lizard。我設法幾乎完全重新創建它,唯一的問題是:玩家2自動獲勝。有人能告訴我我需要更改代碼的位置,並解釋原因嗎?Rock,Paper,Scissor,Spock,python中的蜥蜴,玩家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!") 
+2

那麼你可以忽略所有你的if語句除了倒數第二個,所以如果a是搖滾而b是剪刀,它可能會讓玩家贏得一場勝利 – Sayse

+0

我該如何讓它忽略所有其餘的? – SpartanHero

+0

你已經是,你重新分配了s和我在這一秒的價值觀,以及最後如果陳述,還請注意你的一個條件檢查'scissors' – Sayse

回答

3

你的每個if語句都有一個else。 if語句中只有一個可以爲真,這意味着所有其他語句都被評估。其結果是,最後一個陳述 - 將s設置爲False - 將「獲勝」,因此玩家2獲勝。

你應該放下你的一切陳述,並調整你的代碼作爲一系列if...elif...塊:(注意,如果條件不需要括號)

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

+1

最好也是我會嵌套如果是這樣的操作不必繼續評估,如果一個等於紙:) – Sayse

+0

所以,改變所有如果的ELIF和在最後一個其他? – SpartanHero

相關問題