Reputation: 79
Sorry if this is very newbie question.
I am trying to make a rock, paper, scissor program.
I want to wait for the user to make eather choice r, p, s.
I am not sure why i cant seem to make it work.
When i run my program, it never gets out of the first while loop whatever i type. I dont understand why is it not going on when i enter eather r, p, or s ?
while p1_choice != 'r' or p1_choice != 's' or p1_choice != 'p':
p1_choice = input('Player one please make your choice. Rock(r), Scissors(s), Paper(p): ')
while p2_choice != 'r' or p1_choice != 's' or p1_choice != 'p':
p2_choice = input('Player two please make your choice. Rock(r), Scissors(s), Paper(p): ')
print()
if p1_choice == 'r' and p2_choice == 'p':
print('Player two wins')
elif p1_choice == 'r' and p2_choice == 's':
print('Player one wins')
elif p1_choice == 's' and p2_choice == 'r':
print('Player two wins')
elif p1_choice == 's' and p2_choice == 'p':
print('Player one wins')
elif p1_choice == 'p' and p2_choice == 'r':
print('Player one wins')
elif p1_choice == 'p' and p2_choice == 's':
print('Player two wins')
else:
print('The game is a tie')
Upvotes: 1
Views: 463
Reputation: 490
Here is another way, I think the easiest to read:
def getPlayerChoice() -> str:
choice = '';
while not (choice == 'r' or choice == 's' or choice == 'p'):
choice = input('Please make your choice. Rock(r), Scissors(s), Paper(p): ')
p1_choice = getPlayerChoice();
p2_choice = getPlayerChoice();
Upvotes: 1
Reputation: 7636
This statement p1_choice != 'r' or p1_choice != 's' or p1_choice != 'p':
is always True, hence won't break from the Loop
I strongly suggest you to run the code as follow:
while True:
p1_choice = input('Player one please make your choice. Rock(r), Scissors(s), Paper(p): ')
if p1_choice == 'r' or p1_choice == 's' or p1_choice == 'p':
break
else:
continue
while True:
p2_choice = input('Player two please make your choice. Rock(r), Scissors(s), Paper(p): ')
if p2_choice != 'r' or p1_choice != 's' or p1_choice != 'p':
continue
else:
break
Is more readable and less prone to errors.
Another improvement is, to delegate the check of the User input to a function so:
def check_inputs(user_input):
if user_input == 'r' or user_input == 's' or user_input == 'p':
print('noe')
return True
else:
print('fuckl')
return False
while True:
p1_choice = input('Player one please make your choice. Rock(r), Scissors(s), Paper(p): ')
check = check_inputs(p1_choice)
if check:
break
else:
continue
This follows the DRY Rule
Upvotes: 0
Reputation: 2090
@codewelldev has already nicely pointed out the impossibility to break out of while loop in your code. Since in the OR condition if any condition is True the loop will keep going. You will input one of the following. 'r','s','p'. So because you are negating, there two True(s) and one False at best. It will never finish. May be you want to use 'not in' function.
p1_choice, p2_choice = None, None
while p1_choice not in ('r','s','p'):
p1_choice = input('Player one please make your choice. Rock(r), Scissors(s), Paper(p): ')
while p2_choice not in ('r','s','p'):
p2_choice = input('Player two please make your choice. Rock(r), Scissors(s), Paper(p): ')
print()
if p1_choice == 'r' and p2_choice == 'p':
print('Player two wins')
elif p1_choice == 'r' and p2_choice == 's':
print('Player one wins')
elif p1_choice == 's' and p2_choice == 'r':
print('Player two wins')
elif p1_choice == 's' and p2_choice == 'p':
print('Player one wins')
elif p1_choice == 'p' and p2_choice == 'r':
print('Player one wins')
elif p1_choice == 'p' and p2_choice == 's':
print('Player two wins')
else:
print('The game is a tie')
Upvotes: 2
Reputation: 246
The first while
loop will only stop if p1_choice
is equal to 'r'
, 'p'
, and 's'
, simultaneously, which is impossible.
p1_choice != 'r' or p1_choice != 's' or p1_choice != 'p'
evaluates to True
if p1_choice
does not equal 'r'
or if p1_choice
does not equal 'p'
or if p1_choice
does not equal 's'
.
I believe you meant to use and
instead of or
.
Upvotes: 3