so i was just making a python rock paper scissors game and it has a score limit of 5. so if either the user or the computer reaches the score of 5, the game will print the person won or the computer won respectively. but it prints it if the computer wins only and also when the user wins the game doesn’t end and it doesn’t print that the user won. pls help me this is my code:
import random
user_score = int(0)
pc_score = int(0)
score_limit = 5
while user_score != score_limit or pc_score != score_limit:
user_guess: str = input(str("pls enter your guess, rock/paper/scissors:")).lower()
my_list = "rock", "paper", "scissors"
pc_guess = random.choice(my_list)
print("the computer chose", pc_guess)
if pc_guess == "rock" and user_guess == "rock":
print ( "Tie!!" )
if pc_guess == "paper" and user_guess == "paper":
print ( "Tie!!" )
if pc_guess == "scissors" and user_guess == "scissors":
print ( "Tie!!" )
if pc_guess == "paper" and user_guess == "rock":
print ( "the computer scores" )
pc_score = pc_score + 1
print ( "the pc score is:", pc_score )
if pc_guess == "rock" and user_guess == "paper":
print ( "yay u scored" )
user_score = user_score + 1
print ( "the user score is:", user_score )
if pc_guess == "rock" and user_guess == "scissors":
print ( "the computer scored" )
pc_score = int ( pc_score ) + 1
print ( "the pc score is:", pc_score )
if pc_guess == "scissors" and user_guess == "rock":
print ( "yay u scored" )
user_score = user_score + 1
print ( "the user score is:", user_score )
if pc_guess == "paper" and user_guess == "scissors":
print ( "yay u scored" )
user_score = user_score + 1
print ( "the user score is:", user_score )
if pc_guess == "scissors" and user_guess == "paper":
print ( "the computer scored" )
pc_score = int ( pc_score ) + 1
print ( "the pc score is:", pc_score )
pc_guess = random.choice ( my_list )
if user_score == score_limit:
print("yay congrats u won")
elif pc_score == score_limit:
print("oopsies, the computer won, better luck next time")
break
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
Hi there,
This looks quite good! You just need to move the last if else
statement out of the previous if
statement like this:
import random
user_score = int(0)
pc_score = int(0)
score_limit = 5
while user_score != score_limit or pc_score != score_limit:
user_guess: str = input(str("pls enter your guess, rock/paper/scissors:"))
my_list = "rock", "paper", "scissors"
pc_guess = random.choice(my_list)
print("the computer chose", pc_guess)
if pc_guess == "rock" and user_guess == "rock":
print ( "Tie!!" )
if pc_guess == "paper" and user_guess == "paper":
print ( "Tie!!" )
if pc_guess == "scissors" and user_guess == "scissors":
print ( "Tie!!" )
if pc_guess == "paper" and user_guess == "rock":
print ( "the computer scores" )
pc_score = pc_score + 1
print ( "the pc score is:", pc_score )
if pc_guess == "rock" and user_guess == "paper":
print ( "yay u scored" )
user_score = user_score + 1
print ( "the user score is:", user_score )
if pc_guess == "rock" and user_guess == "scissors":
print ( "the computer scored" )
pc_score = int ( pc_score ) + 1
print ( "the pc score is:", pc_score )
if pc_guess == "scissors" and user_guess == "rock":
print ( "yay u scored" )
user_score = user_score + 1
print ( "the user score is:", user_score )
if pc_guess == "paper" and user_guess == "scissors":
print ( "yay u scored" )
user_score = user_score + 1
print ( "the user score is:", user_score )
if pc_guess == "scissors" and user_guess == "paper":
print ( "the computer scored" )
pc_score = int ( pc_score ) + 1
print ( "the pc score is:", pc_score )
pc_guess = random.choice ( my_list )
if user_score == score_limit:
print("yay the u won")
break
elif pc_score == score_limit:
print("oopsies, the computer won, better luck next time")
break
Hope that this helps! Regards, Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.