I am building a simple rock paper scissors game. Everytime you win, you gain a point. Same for the computer. Between the player and computer, the first to get 3 points wins. Everything worked fine but I wanted to add an extra feature which is an option to play again.
import random
user_name_input = input("What is your name?: ")
print(f"Oh hello {user_name_input}!")
options = ["rock", "paper", "scissors"]
user_points = 0
comp_points = 0
while True:
print("ROCK--PAPER--SCISSORS!")
user_choice = input("SHOOT!: ").lower()
comp_choice = random.choice(options)
if user_choice not in options:
print("Please choose either rock, paper or scissors")
print("\n")
continue
# CONDITIONS
if user_choice == "rock" and comp_choice == "scissors":
print("WIN")
print(f"Computer's Choice: {comp_choice}")
user_points = 1
print(f"Your Points: {user_points}")
print(f"Computer's Points: {comp_points}")
print("\n")
elif user_choice == "paper" and comp_choice == "rock":
print("WIN")
print(f"Computer's Choice: {comp_choice}")
user_points = 1
print(f"Your Points: {user_points}")
print(f"Computer's Points: {comp_points}")
print("\n")
elif user_choice == "scissors" and comp_choice == "paper":
print("WIN")
print(f"Computer's Choice: {comp_choice}")
user_points = 1
print(f"Your Points: {user_points}")
print(f"Computer's Points: {comp_points}")
print("\n")
elif user_choice == comp_choice:
print(f"Computer's Choice: {comp_choice}")
print("Same picks, play again")
print(f"Your Points: {user_points}")
print(f"Computer's Points: {comp_points}")
print("\n")
else:
print("LOSS")
print(f"Computer's Choice: {comp_choice}")
comp_points = 1
print(f"Your Points: {user_points}")
print(f"Computer's Points: {comp_points}")
print("\n")
if user_points == 3:
print("You've won")
print(
f"You did it {user_name_input}!, you're a rock, paper, scissors MASTER!!!!")
quit()
elif comp_points == 3:
print("You've lost")
print(f"Sorry {user_name_input}, you'll get it next time!")
quit()
I added this condition at the bottom, and I expected that the using the "continue" statement, python would loop back to the beginning of the while loop, and the game would run again. However this messed up my program entirely and now when I run it, after just one play, the computer asks the user if they want to play again. It's not looping 3 times like the original.
if play_again == "yes":
print("Great! Let's go!")
continue
else:
play_again == "no"
print("Ok thanks for playing")
quit()
Does anyone know why? Thank you.
CodePudding user response:
Short answer: You have to reset user_points and comp_points back to zero. loops don't reset values that are declared outside the loop.
If play_again == "yes", then the compiler re executes from the top. But in your loop, you only reset user_choice
and comp_choice
to a new value, so user_points
and comp_points
stays the same.
Lets assume in the last game the user won, which means user_points
is 3. Now if the player continues to play, user_points
still has the value of 3. Now it will still let you play one round since you didn't check the points at the start. Now when you finish the first round and you check the points, user_points
is still 3, so the loop will end.
To fix this you need to change:
if play_again == "yes":
print("Great! Let's go!")
continue
else:
play_again == "no"
print("Ok thanks for playing")
quit()
to
if play_again == "yes":
print("Great! Let's go!")
user_points = 0
comp_points = 0
continue
else:
play_again == "no"
print("Ok thanks for playing")
quit()
CodePudding user response:
You can add all in an function import random
def game():
user_name_input = input("What is your name?: ")
print(f"Oh hello {user_name_input}!")
options = ["rock", "paper", "scissors"]
user_points = 0
comp_points = 0
while True:
# All your while loop
play_again = input("Do you want to play again?: ")
if play_again == "yes":
print("Great! Let's go!")
game() # This calls again your function
elif play_again == "no":
print("Ok thanks for playing")
game()