2017-09-22 122 views
-3

我剛剛完成了我的第一個學期的編程任務;製作一張搖滾紙,剪刀遊戲持續到一個玩家贏得三次。我已經設法讓它運行,但是我很難讓它回到起點。如何在Python中循環初學者

ps1 = 0 
ps2 = 0 


from random import radint 
player1 = imput('R, P, S') 

print (player1, " vs ", end='') 
chosen = randint(1,3) 
    #print (chosen) 
if chosen == 1: 
    player2 = 'R' 
elif chosen == 2: 
    player2 = 'P' 
else: 
    player2 = 'S' 

print(player2) 

if player1 == player2: 
    print("Draw") 
elif player1 == 'R' and player2 == 'S' 
    ps1 = ps1 + 1 
     print("Point to Player 1") 
elif player1 == 'R' and player2 == 'P' 
    ps2 = ps2 + 1 
     print("Point to Player 2") 
elif player1 == 'P' and player2 == 'S' 
    ps2 = ps2 + 1 
     print("Point to Player 2") 
elif player1 == 'P' and player2 == 'R' 
    ps1 = ps1 + 1 
     print("Point to Player 1") 
elif player1 == 'S' and player2 == 'R' 
    ps2 = ps2 + 1 
     print("Point to Player 2") 
elif player1 == 'S' and player2 == 'P' 
    ps1 = ps1 + 1 
     print("Point to Player 1") 
if ps1 = 3 
    print("Player 1 wins!") 
elif ps2 = 3 
    print("Player 2 wins!") 
+0

[試試這個](https://www.google.com/search?q=python+loops) –

回答

1

您需要一個while循環來檢查每次重新開始時是否有贏家。當你在循環中找到一個贏家時,讓while條件成爲False,這樣它就不會再循環。或者就像下面提到的那樣「休息」。

+0

或者只是使用'break',對吧? – toonarmycaptain

+0

是的。這樣更好 –