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

while rounds > player_point or cpu_point:

nor

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.

You can try

while rounds > max(player_point, cpu_point)

Two things.

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.

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?

Thanks for the replies. Max completely slipped my mind. The game works as intended now.

Here's the whole thing:

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.

while int_rounds > win or int_rounds > loss:

This is the code I was trying to use.

while int_rounds > win or int_rounds > loss:

Yes, you should have written 'and' istead of 'or'.

>>> #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

Edit a little to late i see:icon_wink:

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 :icon_biggrin:

You can simply use this little test ...

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

I usually design the while loop this way, it makes the logic easier to follow ...

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.