2014-01-05 53 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

非常感谢!如果有领带,你能帮助关于重新启动功能的部分吗? –