| | |
while X > Y or Z
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
I'm making a small game in which you can choose the amount of points needed for victory. You play until either cpu_point or player_point is equal to (or greater than) the rounds. You get one point per win.
What I'm having trouble with is the while check. I'd like to shorten the code by using "or". Either I've completely misunderstood the use of "or" in Python, or my syntax is wrong.
Neither
nor
gives me the desired effect.
The former just keeps playing endlessly, while the latter is acting like an "AND", meaning that both cpu_point and player_point have to reach the mark before the game ends.
The solution is probably really obvious to seasoned programmers, but I'm still learning the basics.
Thanks for reading.
What I'm having trouble with is the while check. I'd like to shorten the code by using "or". Either I've completely misunderstood the use of "or" in Python, or my syntax is wrong.
Neither
Python Syntax (Toggle Plain Text)
while rounds > player_point or cpu_point:
Python Syntax (Toggle Plain Text)
while rounds > player_point or rounds > cpu_point:
The former just keeps playing endlessly, while the latter is acting like an "AND", meaning that both cpu_point and player_point have to reach the mark before the game ends.
The solution is probably really obvious to seasoned programmers, but I'm still learning the basics.
Thanks for reading.
Last edited by Yeen; Nov 9th, 2009 at 4:15 pm.
0
#2 Nov 9th, 2009
You can try
python Syntax (Toggle Plain Text)
while rounds > max(player_point, cpu_point)
•
•
Join Date: Jan 2008
Posts: 8
Reputation:
Solved Threads: 2
0
#3 Nov 9th, 2009
Two things.
This goes on forever because of the order of operator precedence. In this case, comparison operators are checked before boolean operators. So on the first move python will convert it to say: while <True | False> or cpu_point. The true | false simply signifying that it may be true or false, it doesn't honestly matter. The next move it sees is to do <Boolean value> or <integer value>. Since or requires each side be a boolean value, it will convert the integer value into a boolean value. If the interger value is 0, then it will become false, if the integer value is ANYTHING other than 0, it will become true.
Thus your equation is evaluating to either True or False, True or True, or False or True. It can never be false or false - which would kick you from the loop. I hope this all made sense.
Now for the second equation.
You have left out your additional code (like the starting values for round, player_point, and cpu_point and when (and by how much) these values get incremented.
The while loop itself (assuming rounds is the number of rounds a player must win) looks fine, with the exception that you say in your description "greater than or equal to" but your program is only checking greater than.
My guess is that something is not incrementing correctly or you are accidentally resetting a value in the loop somewhere. Could you post the rest of your loop code so we can see what is going on?
python Syntax (Toggle Plain Text)
while rounds > player_point or cpu_point:
This goes on forever because of the order of operator precedence. In this case, comparison operators are checked before boolean operators. So on the first move python will convert it to say: while <True | False> or cpu_point. The true | false simply signifying that it may be true or false, it doesn't honestly matter. The next move it sees is to do <Boolean value> or <integer value>. Since or requires each side be a boolean value, it will convert the integer value into a boolean value. If the interger value is 0, then it will become false, if the integer value is ANYTHING other than 0, it will become true.
Thus your equation is evaluating to either True or False, True or True, or False or True. It can never be false or false - which would kick you from the loop. I hope this all made sense.
Now for the second equation.
python Syntax (Toggle Plain Text)
while rounds > player_point or rounds > cpu_point:
You have left out your additional code (like the starting values for round, player_point, and cpu_point and when (and by how much) these values get incremented.
The while loop itself (assuming rounds is the number of rounds a player must win) looks fine, with the exception that you say in your description "greater than or equal to" but your program is only checking greater than.
My guess is that something is not incrementing correctly or you are accidentally resetting a value in the loop somewhere. Could you post the rest of your loop code so we can see what is going on?
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
1
#4 Nov 9th, 2009
Thanks for the replies. Max completely slipped my mind. The game works as intended now.
Here's the whole thing:
This is the code I was trying to use.
Here's the whole thing:
Python Syntax (Toggle Plain Text)
import random rock, paper, scissors = 1, 2, 3 names = "rock", "paper", "scissors" win = 0 loss = 0 print("\nWelcome to the Rock, Paper, Scissors game.\nPlease choose the amount of points requited to win.") rounds = input() int_rounds = int(rounds) def checkResults(user, computer): if (user, computer) in ((rock, paper), (paper, scissors), (scissors, rock)): return False return True while int_rounds > max(win, loss): # This is where I was using "or" print("\nChoose your weapon:") print("1: Rock") print("2: Paper") print("3: Scissors") player = int(input()) cpu = random.choice((rock, paper, scissors)) print("You chose",names[player-1],", the computer chose",names[cpu-1]) if cpu != player: if checkResults(player, cpu): print ("Player won!") win = win + 1 else: print ("Computer won!") loss = loss + 1 else: print ("Draw!") print("\nFinal Score:") print("Player:",win) print("Computer:",loss)
This is the code I was trying to use.
Python Syntax (Toggle Plain Text)
while int_rounds > win or int_rounds > loss:
0
#5 Nov 9th, 2009
•
•
•
•
This is the code I was trying to use.
Python Syntax (Toggle Plain Text)
while int_rounds > win or int_rounds > loss:
0
#6 Nov 9th, 2009
Python Syntax (Toggle Plain Text)
>>> #Is rounds greater than player_point or cpu_point >>> #It will print Victory >>> #Test >>> rounds = 5 >>> player_point = 4 >>> cpu_point = 3 >>> while rounds > player_point or cpu_point: print 'Victory' break else: print 'Lose' Victory >>> #So if we change player_point to 8 >>> player_point = 8 >>> while rounds > player_point or cpu_point: print 'Victory' break else: print 'Lose' Victory >>> #It still print Victory because of "or" >>> #If we change to "and" then Lose >>> while rounds > player_point and cpu_point: print 'Victory' break else: print 'Lose' Lose >>> player_point = 4 >>> while rounds > player_point and cpu_point: print 'Victory' break else: print 'Lose' Victory >>> #Last one Victory beacause now both player_point and cpu_point are less than rounds
Last edited by snippsat; Nov 9th, 2009 at 4:55 pm.
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#7 Nov 9th, 2009
•
•
•
•
This goes on forever because of the order of operator precedence. In this case, comparison operators are checked before boolean operators. So on the first move python will convert it to say: while <True | False> or cpu_point. The true | false simply signifying that it may be true or false, it doesn't honestly matter. The next move it sees is to do <Boolean value> or <integer value>. Since or requires each side be a boolean value, it will convert the integer value into a boolean value. If the interger value is 0, then it will become false, if the integer value is ANYTHING other than 0, it will become true.
Thus your equation is evaluating to either True or False, True or True, or False or True. It can never be false or false - which would kick you from the loop. I hope this all made sense.
•
•
•
•
Yes, you should have written 'and' istead of 'or'.
•
•
•
•
Edit a little to late i see
0
#8 Nov 10th, 2009
You can simply use this little test ...
I usually design the while loop this way, it makes the logic easier to follow ...
Python Syntax (Toggle Plain Text)
int_rounds = 3 win = 0 loss = 0 print(int_rounds > max(win, loss)) # True win = 0 loss = 0 print(int_rounds > win or int_rounds > loss) # True win = 0 loss = 0 print(int_rounds > win and int_rounds > loss) # True win = 4 loss = 0 print(int_rounds > win or int_rounds > loss) # True win = 4 loss = 0 print(int_rounds > win and int_rounds > loss) # False --> loop exits
Python Syntax (Toggle Plain Text)
int_rounds = 3 win = 0 loss = 0 while True: if int_rounds > win: break if int_rounds > loss: break # now use if condition to add to win or loss # (for instance you keep winning) win += 1
May 'the Google' be with you!
![]() |
Other Threads in the Python Forum
- Previous Thread: creating a new EMPTY array
- Next Thread: Readlines() into an array
Views: 308 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Python
anti array avogadro beginner builtin clear client code color count csv curved def dictionary dynamic enter examples excel file float format frange ftp function gui heads hints homework import input java lapse line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pyopengl pyqt python random raw_input recursion recursive redirect script scrolledtext singleton software sqlite ssh stderr string strings subprocess sum syntax table terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape wikipedia windows word wx.wizard wxpython






