如果某些值,python循环再次?

时间:2014-01-05 20:36:40

标签: python loops

我正在尝试制作一个与用户一起播放“Rock Paper Scissors Lizard Spock”​​的程序。我已经完成了大部分工作,但我遇到的一个问题是,如果游戏达成平局,我希望忽略这一结果,并重复循环。换句话说,我希望有三个输赢的结果。另外,为了完全搞砸我刚刚说的话,我希望如果用户连续两次获胜或者程序连续两次获胜,它会完全退出循环。

对于第一个问题,我尝试使用continue,但这根本不起作用。我在这个网站上经历过几个岩石剪刀问题,但我没有看到任何适用于此的答案。第一个问题具有优先权;第二个只是顶部的樱桃。

这是我的代码(有点长,我很抱歉):

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的初学者。

1 个答案:

答案 0 :(得分: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:

进行检查