Reputation: 89
I have a game that goes through rounds, which have the variables, count1
and count2
as score. I am trying to create some logic that plays the while loop till someone reaches 9, but also has to be 2 points ahead.
I feel like I'm getting close. I'm pretty sure the first portion's right. I keep trying to change the Boolean and format to see if I can get it to work.
while (count1 <= 9 and count2 <= 9) and ((count1 - count2 > 2) or (count2 - count1 > 2)):
Happy Path: The while
loop loops through till someone gets to 9 and has a 2 point lead.
As far as errors go, it keeps going to end game since that is the else statement, which tells me that my logic is still a bit off.
Upvotes: 1
Views: 63
Reputation: 40036
Let's deal with it bit-by-bit:
You want the loop keep running. When someone hit 9 points or more, and score difference is 2 or more, stop the loop.
Easiest translation will be:
# s1 and s2 representing score 1 and 2
while True:
if (s1 >= 9 or s2 >= 9) and abs(s1 - s2) >= 2:
break
# do something
Which also implies, when the condition is NOT met, we keep going:
# s1 and s2 representing score 1 and 2
while not ((s1 >= 9 or s2 >= 9) and abs(s1 - s2) >= 2):
# do something
Some basic boolean arithmetic simplify the condition:
# s1 and s2 representing score 1 and 2
while (s1 < 9 and s2 < 9) or abs(s1 - s2) < 2:
# do something
For which the condition also reads correct: we keep looping while both scores are under 9, or (at least one reaches 9 and) score difference is less than 2.
Upvotes: 1
Reputation: 59228
You are keeping the loop running if one is ahead by 2 points while I believe you want the opposite:
while (count1 <= 9 and count2 <= 9) and ((count1 - count2 > 2) or (count2 - count1 > 2)):
I haven't seen the rest of your code, but this should probably work:
while (count1 < 9 and count2 < 9) or abs(count1 - count2) < 2:
Upvotes: 3