2014-01-05 80 views
1

我正在試圖製作一個與用戶一起播放「Rock Paper Scissors Lizard Spock」​​的程序。我已經完成了大部分工作,但我遇到的一個問題是,如果遊戲達到平局,我希望忽略這一結果,並重復循環。另一種說法是,我希望有三項輸贏結果。另外,爲了完全解決我剛纔所說的問題,如果用戶在連續兩次獲勝或者連續兩次獲得勝利,它會完全退出循環。python循環再次如果某些值?

對於第一個問題,我嘗試使用繼續,但這根本不起作用。我在這個網站上經歷了幾個搖滾紙剪刀的問題,但是我沒有看到任何適用於此的答案。第一個問題是重中之重;第二隻是頂級的櫻桃。

這裏是我的代碼(這是一個有點長,我很抱歉):

import random 
Rock = "Rock" 
Paper = "Paper" 
Scissors = "Scissors" 
Lizard = "Lizard" 
Spock = "Spock" 
Words = (Rock, Paper, Scissors, Lizard, Spock) 


def The_Game (x): 
    for i in range(3): 
     y = random.choice(Words) 
     x = input("Choose your weapon! Rock, paper, scissors, lizard, or spock?") 
     if x == y: 
      print(y) 
      print("Tie. Try again!") 
     else: 
      if x == Rock: 
       if y == Paper: 
        print(y) 
        print("Paper covers Rock. You lost this round!") 
       elif (y == Spock): 
        print(y) 
        print("Spock vaporizes Rock. You lost this round!") 
       elif (y == Lizard): 
        print(y) 
        print("Rock crushes Lizard. You won this round!") 
       else: 
        print(y) 
        print("As always, Rock crushes Scissors. You won this round!") 
      elif (x == Paper): 
       if y == Scissors: 
        print(y) 
        print("Scissors cut Paper. You lost this round!") 
       elif (y == Lizard): 
        print(y) 
        print("Lizard eats paper. You lost this round!") 
       elif (y == Rock): 
        print(y) 
        print("Paper covers Rock. You won this round!") 
       else: 
        print(y) 
        print("Paper disproves Spock. You won this round!") 
      elif (x == Scissors): 
       if y == Rock: 
        print(y) 
        print("As always, Rock crushes Scissors. You lost this round!") 
       elif (y == Spock): 
        print(y) 
        print("Spock melts scissors. You lost this round!") 
       elif (y == Paper): 
        print(y) 
        print("Scissors cut Paper. You won this round!") 
       else: 
        print(y) 
        print("Scissors decapitate Lizard. You won this round!") 
      elif (x == Lizard): 
       if y == Rock: 
        print(y) 
        print("Rock crushes Lizard. You lost this round!") 
       elif (y == Scissors): 
        print(y) 
        print("Scissors decapitate Lizard. You lost this round!") 
       elif (y == Paper): 
        print(y) 
        print("Lizard eats Paper. You won this round!") 
       elif (y == Spock): 
        print(y) 
        print("Lizard poisons Spock. You won this round!") 
      else: 
       if y == Paper: 
        print(y) 
        print("Paper disproves Spock. You lost this round!") 
       elif (y == Lizard): 
        print(y) 
        print("Lizard poisons Spock. You lost this round!") 
       elif (y == Scissors): 
        print(y) 
        print("Spock melts scissors. You won this round!") 
       else: 
        print(y) 
        print("Spock vaporizes Rock. You won this round!") 


x = ("Let's play Rock Paper Scissors Lizard Spock! Best 2 of three!") 
print(x) 
The_Game(x) 

Yes = "Yes" 
No = "No" 
p = input("Play again? (Yes or No) ") 
    if p == Yes: 
     The_Game(x) 
    else: 
     print("Thanks for playing!") 

如果有人可以幫助,那將是美好的!我只是一個Python初學者。

回答

0

好的,讓我們先稍微改變你的代碼。不要讓TheGame()玩3次,讓我們只玩一次。

然後,一旦我們這樣做了,我們可以讓每個呼叫TheGame()都有一個有用的返回值。如果用戶贏了,那麼返回一個有用的值。展示這種方法的一種方式是如果玩家獲勝,則執行return True,否則執行return False

例如:

if x == Rock and y == Paper: 
    print(y) 
    print("Paper covers Rock. You lost this round!") 
    return False 

然後,你的主循環可能會成爲這樣的事情:

print("Let's play Rock Paper Scissors Lizard Spock! Best 2 of three!") 

win_count = 0 
loss_count = 0 
while win_count < 2 and loss_count < 2: 
    if The_Game(x) == True: 
     win_count = win_count + 1 
    else: 
     loss_count = loss_count + 1 

if win_count == 2: 
    print("You won the set!") 
else: 
    print("You lost the set!") 

一個注意:您可能要檢查用戶輸入的是你想要的範圍內。您可以使用以下條件來檢查:if x not in Words:

+0

他在「if x == y:」塊中包含了兩個選擇相同的情況。 – senshin

+0

@senshin:哎呦。謝謝。 –

+0

非常感謝!如果有領帶,你能幫助關於重新啓動功能的部分嗎? –