while X > Y or Z

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 18
Reputation: Yeen is an unknown quantity at this point 
Solved Threads: 0
Yeen Yeen is offline Offline
Newbie Poster

while X > Y or Z

 
0
  #1
Nov 9th, 2009
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
  1. while rounds > player_point or cpu_point:
nor
  1. while rounds > player_point or rounds > cpu_point:
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.
Last edited by Yeen; Nov 9th, 2009 at 4:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 233
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #2
Nov 9th, 2009
You can try
  1. while rounds > max(player_point, cpu_point)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 8
Reputation: erees is an unknown quantity at this point 
Solved Threads: 2
erees erees is offline Offline
Newbie Poster
 
0
  #3
Nov 9th, 2009
Two things.

  1. 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.

  1. 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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: Yeen is an unknown quantity at this point 
Solved Threads: 0
Yeen Yeen is offline Offline
Newbie Poster
 
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:

  1. import random
  2.  
  3. rock, paper, scissors = 1, 2, 3
  4. names = "rock", "paper", "scissors"
  5. win = 0
  6. loss = 0
  7.  
  8. print("\nWelcome to the Rock, Paper, Scissors game.\nPlease choose the amount of points requited to win.")
  9.  
  10. rounds = input()
  11. int_rounds = int(rounds)
  12.  
  13. def checkResults(user, computer):
  14. if (user, computer) in ((rock, paper), (paper, scissors), (scissors, rock)):
  15. return False
  16. return True
  17.  
  18. while int_rounds > max(win, loss): # This is where I was using "or"
  19. print("\nChoose your weapon:")
  20. print("1: Rock")
  21. print("2: Paper")
  22. print("3: Scissors")
  23.  
  24. player = int(input())
  25. cpu = random.choice((rock, paper, scissors))
  26.  
  27. print("You chose",names[player-1],", the computer chose",names[cpu-1])
  28.  
  29. if cpu != player:
  30. if checkResults(player, cpu):
  31. print ("Player won!")
  32. win = win + 1
  33. else:
  34. print ("Computer won!")
  35. loss = loss + 1
  36. else:
  37. print ("Draw!")
  38.  
  39. print("\nFinal Score:")
  40. print("Player:",win)
  41. print("Computer:",loss)


This is the code I was trying to use.
  1. while int_rounds > win or int_rounds > loss:
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 233
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #5
Nov 9th, 2009
Originally Posted by Yeen View Post
This is the code I was trying to use.
  1. while int_rounds > win or int_rounds > loss:
Yes, you should have written 'and' istead of 'or'.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 184
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 64
snippsat's Avatar
snippsat snippsat is online now Online
Junior Poster
 
0
  #6
Nov 9th, 2009
  1. >>> #Is rounds greater than player_point or cpu_point
  2. >>> #It will print Victory
  3. >>> #Test
  4. >>> rounds = 5
  5. >>> player_point = 4
  6. >>> cpu_point = 3
  7. >>> while rounds > player_point or cpu_point:
  8. print 'Victory'
  9. break
  10. else:
  11. print 'Lose'
  12.  
  13.  
  14. Victory
  15. >>> #So if we change player_point to 8
  16. >>> player_point = 8
  17. >>> while rounds > player_point or cpu_point:
  18. print 'Victory'
  19. break
  20. else:
  21. print 'Lose'
  22.  
  23.  
  24. Victory
  25. >>> #It still print Victory because of "or"
  26. >>> #If we change to "and" then Lose
  27. >>> while rounds > player_point and cpu_point:
  28. print 'Victory'
  29. break
  30. else:
  31. print 'Lose'
  32.  
  33.  
  34. Lose
  35. >>> player_point = 4
  36. >>> while rounds > player_point and cpu_point:
  37. print 'Victory'
  38. break
  39. else:
  40. print 'Lose'
  41.  
  42.  
  43. Victory
  44. >>> #Last one Victory beacause now both player_point and cpu_point are less than rounds
Edit a little to late i see
Last edited by snippsat; Nov 9th, 2009 at 4:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: Yeen is an unknown quantity at this point 
Solved Threads: 0
Yeen Yeen is offline Offline
Newbie Poster
 
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.
It'll probably make sense in the morning after I've had some sleep. I'm not thinking straight at the moment. Definitely giving that another read tomorrow.

Yes, you should have written 'and' istead of 'or'.
I just tried it out and it works. Somehow this has me very puzzled. Usually when I see "AND" I know that both values have to be true, and when I see "OR" either or both value(s) can be true. Is Python messing with my head?

Edit a little to late i see
Appreciate the reply nonetheless
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,161
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 980
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #8
Nov 10th, 2009
You can simply use this little test ...
  1. int_rounds = 3
  2.  
  3. win = 0
  4. loss = 0
  5. print(int_rounds > max(win, loss)) # True
  6.  
  7. win = 0
  8. loss = 0
  9. print(int_rounds > win or int_rounds > loss) # True
  10.  
  11. win = 0
  12. loss = 0
  13. print(int_rounds > win and int_rounds > loss) # True
  14.  
  15. win = 4
  16. loss = 0
  17. print(int_rounds > win or int_rounds > loss) # True
  18.  
  19. win = 4
  20. loss = 0
  21. print(int_rounds > win and int_rounds > loss) # False --> loop exits
I usually design the while loop this way, it makes the logic easier to follow ...
  1. int_rounds = 3
  2.  
  3. win = 0
  4. loss = 0
  5. while True:
  6. if int_rounds > win:
  7. break
  8. if int_rounds > loss:
  9. break
  10. # now use if condition to add to win or loss
  11. # (for instance you keep winning)
  12. win += 1
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum


Views: 308 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC