Reputation: 35
I am writing a program for counting the score of two users. The game ends when either of them scores a ten and the respective player wins.
I wrote the while loop as:
while (score1 != 10) or (score2 != 10):
...
and my program does not terminate.
Here is the code:
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))
Upvotes: 2
Views: 197
Reputation: 129477
Looks like you want
while (score1 != 10) and (score2 != 10):
since you want the loop to end as soon as either one of the scores reaches 10
, at which point score != 10
will be false
and, consequently, the entire loop-condition will no longer be satisfied.
(score1 != 10) or (score2 != 10)
would require both scores to be 10
before exiting.
Upvotes: 3