:D I've make a little rock paper scissors game in Python just for fun and practice and I've been trying to implement a little scoring system that doesn't seem to want to work properly and I'm not sure how to solve the problem.
import random
import messages
def gameOn():
choice = ["rock", "paper", "scissors"]
computer_choice = random.choice(choice)
player_choice = input("Please chose Rock/Paper/Scissors: ")
player_choice = player_choice.lower()
total_score = 0
while True:
#Makes sure the user enters a valid option.
if player_choice not in("rock", "paper", "scissors"):
print("Choice is not correct!")
#Prints in case both the computer and the player chose the same option.
elif computer_choice == player_choice:
print("You chose the same.")
#Computer choses ROCK.
elif computer_choice == "rock" and player_choice == "paper":
print(messages.win[0])
total_score += 1
print(total_score)
elif computer_choice == "rock" and player_choice == "scissors":
print(messages.lose[0])
#Computer choses PAPER.
elif computer_choice == "paper" and player_choice == "rock":
print(messages.lose[1])
elif computer_choice == "paper" and player_choice == "scissors":
print(messages.win[1])
total_score += 1
print(total_score)
#Computer choses SCISSORS.
elif computer_choice == "scissors" and player_choice == "rock":
print(messages.win[2])
total_score += 1
print(total_score)
elif computer_choice == "scissors" and player_choice == "paper":
print(messages.lose[2])
#Asks the user if he/she wants to play again and restarts the loop if so.
answer = input("Would you like to play again or see your score? Yes/No/Score ")
if answer in ("yes", "Yes", "y", "Y", "yup"):
print("Game starting again!")
gameOn()
elif answer in ("Score", "score"):
print("Your total score is " + str(total_score))
answer = input("Would you like to play again or see your score? Yes/No/Score ")
print("Game starting again!")
gameOn()
else:
print("Goodbye!")
break
gameOn()
The incrementation in itself works but what I want to do is, if the player wins multiple round and at the end writes "score" he should be able to see all the points he has earned. At the moment the score variable get reset every time a new game starts so the score of the use is always either 0 or 1 if he won the round. How could I prevent this behavior? Thank you very much :D and I hope it's not a too stupid question.
I would not advise using recursion for this. You also shouldn't need to use global variables.
I would rather just loop with while True
in your gameOn
function and break if the player wants to exit, so that your score is kept in scope.
Also your "Choice is not correct!" was not working because of the indentation.
import random
import messages
def gameOn():
choice = ["rock", "paper", "scissors"]
total_score = 0
while True:
computer_choice = random.choice(choice)
player_choice = input("Please chose Rock/Paper/Scissors: ")
player_choice = player_choice.lower()
while True:
#Makes sure the user enters a valid option.
if player_choice in("rock", "paper", "scissors"):
break
print("Choice is not correct!")
#Prints in case both the computer and the player chose the same option.
elif computer_choice == player_choice:
print("You chose the same.")
#Computer choses ROCK.
elif<computer_choice == "rock" and player_choice == "paper":
print(messages.win[0])
total_score += 1
print(total_score)
elif computer_choice == "rock" and player_choice == "scissors":
print(messages.lose[0])
#Computer choses PAPER.
elif computer_choice == "paper" and player_choice == "rock":
print(messages.lose[1])
elif computer_choice == "paper" and player_choice == "scissors":
print(messages.win[1])
total_score += 1
print(total_score)
#Computer choses SCISSORS.
elif computer_choice == "scissors" and player_choice == "rock":
print(messages.win[2])
total_score += 1
print(total_score)
elif computer_choice == "scissors" and player_choice == "paper":
print(messages.lose[2])
#Asks the user if he/she wants to play again and restarts the loop if so.
answer = input("Would you like to play again or see your score? Yes/No/Score ")
if answer in ("Score", "score"):
print("Your total score is " + str(total_score))
answer = input("Would you like to play again or see your score? Yes/No/Score ")
print("Game starting again!")
elif answer in ("yes", "Yes", "y", "Y", "yup"):
print("Game starting again!")
else:
print("Goodbye!")
break #this makes your game stop
gameOn()