2013-07-11 126 views
2

我正在編寫一個程序來計算兩個用戶的分數。當他們中的任何一個得分爲10並且相應的玩家獲勝時遊戲結束。python雖然循環不終止

我寫的while循環:

while (score1 != 10) or (score2 != 10): 
    ... 

和我的程序不會終止。

下面是代碼:

player1 = input("Enter name for Player1") 
player2 = input("Enter name for Player2") 
score1=0 
score2=0 


print ("Score for Player1 is: %d,Score for player2 is :%d" %(score1,score2)) 

while (score1 != 10) or (score2 != 10): 
    player =input("enter name for player") 

    if player is player1: 
     score1=score1+1 
    if player is player2: 
     score2=score2+1 
    print ("Score for Player1 is: %d,Score for player2 is :%d" %(score1,score2)) 

回答

2

看起來像你想

while (score1 != 10) and (score2 != 10): 

,因爲你想要的分數的一方達到10循環儘快結束,在這一點score != 10會爲false,因此,整個循環條件將不再滿足。

(score1 != 10) or (score2 != 10)需要得分爲10退出前。

+1

謝謝,這有幫助。:) – RamyaV