Brendan.Sherman Brendan.Sherman - 2 months ago 10
Python Question

While loop is not breaking when condition is met, What am I doing wrong?

The program I am writing is a Python 3.5 rocks paper scissors game that plays rocks paper scissors until the cpu or player gets to a score of 5. The best way I could think of to do this was in a while loop, but by some mistake of mine the games continue infinitely and the loop does not break when the score becomes 5. I'll paste the code here(I hope my comments are helpful.) Thanks in advance for the help, I know this is very simple and silly!

#import the "random" library
import random
#Welcome the user and explain the program
print("Welcome to the Rock Paper Scissor game! You will play until you or the computer gets 5 wins!")


#Set constants for rock paper and scissors for the computer's choice
ROCK = 1
PAPER = 2
SCISSOR = 3


#Define the scores for the user and computer and start them as 0
user_score = 0
cpu_score = 0

#Start the loop that runs until the user or computer reaches the score of 5
while user_score != 5 or cpu_score != 5:
#Gets the choice of the user and stores it in a variable
choice = input("Please enter your choice- 'r'ock, 'p'aper, or 's'cissors? \n")
#Prevents the loop from progressing until the user picks a valid command
while choice != "r" and choice != "p" and choice != "s":
choice = input("Invalid command: Please enter your choice- 'r'ock, 'p'aper, or 's'cissors? \n")
#get's a random pick between 1 and 3 for the cpu's choice
cpu_pick = random.randint(ROCK,SCISSOR)

#Prints the pick of the user prior to determining the winner so this does not have to be included in every if statement
if choice == "r":
print("You pick rock!")
elif choice == "s":
print("You pick scissors!")
elif choice == "p":
print("You pick paper!")

#Prints the pick of the cpu prior to determining the winner so this does not have to be included in every if statement
if cpu_pick == ROCK:
print("The cpu picks a rock!\n")
elif cpu_pick == SCISSOR:
print("The cpu picks scissors!\n")
elif cpu_pick == PAPER:
print("The cpu picks paper!\n")

#Accounts for all cases when the cpu pick is rock and adds to the scores when somebody wins
if cpu_pick == ROCK and choice == "r":
print("Tie! New round!")
elif cpu_pick == ROCK and choice == "s":
print("CPU wins this round!")
cpu_score += 1
elif cpu_pick == ROCK and choice == "p":
print("You win this round!")
user_score += 1

#Accounts for all cases when the cpu pick is Scissors and adds to the scores when somebody wins
if cpu_pick == SCISSOR and choice == "s":
print("Tie! New round!")
elif cpu_pick == SCISSOR and choice == "p":
print("CPU wins this round!")
cpu_score += 1
elif cpu_pick == SCISSOR and choice == "r":
print("You win this round!")
user_score += 1

# Accounts for all cases when the cpu pick is Paper and adds to the scores when somebody wins
if cpu_pick == PAPER and choice == "p":
print("Tie! New round!")
elif cpu_pick == PAPER and choice == "r":
print("CPU wins this round!")
cpu_score += 1
elif cpu_pick == PAPER and choice == "s":
print("You win this round!")
user_score += 1

#Prints the score after each round
print("Score: \nComputer: %d\nYou: %d" % (cpu_score, user_score))

#when the loop is broken check who won then print the final score and winner
if user_score == 5:
print("You win by a score of %d to %d!" % (user_score, cpu_score))
elif user_score == 5:
print("The CPU won bu a score of %d to %d!" % (cpu_score, user_score))

Answer

Use and instead of or in the while condition:

while user_score != 5 and cpu_score != 5:
Comments