Search code examples
pythonnew-operator

Python simple rock, paper, scissors game


Hey so I'm a starting programmer and for school I had to make a simple rock, paper, scissors game (best out of 3) in python. My issue is with the score system, I get the two following errors:

Traceback (most recent call last):
  File "c:\Users\Fabian\OneDrive\School\Python\Project stuff\RockPaperScissors\game.py", line 66, in <module>
    game1()
  File "c:\Users\Fabian\OneDrive\School\Python\Project stuff\RockPaperScissors\game.py", line 26, in game1
    score_com = score_com + 1
UnboundLocalError: local variable 'score_com' referenced before assignment

And this is the code I have thus far:

import random

#variables
rock = 1
paper = 2
scissors = 3

#empty score
score = 0
score_com = 0

#game info
print("Rules and how to play: ")
print("The game is best out of 3")
print("If you wanna choose rock type 1, if you wanna choose paper type 2 and if you wanna use scissors type 3")
print("Dont worry if you forget what number represents what choice, there will be a reminder before you input a number")

#how the game works
def game1():
    
    choice = input("Choose: rock, paper or scissors (1, 2 or 3)")
    if choice == "1":
        choice_com = random.randint(1, 3)
        print(choice_com)

        if choice_com == 1:
            print("Tie!")
        if choice_com == 2:
            print("You lost!")
            score_com = score_com + 1
        if choice_com == 3:
            print("You win!")
            score = score + 1

    elif choice == "2":
        choice_com = random.randint(1, 3)
        print(choice_com)

        if choice_com == 1:
            print("You win!")
        if choice_com == 2:
            print("Tie!")
            score_com = score_com + 1
        if choice_com == 3:
            print("You lost!")
            score = score + 1

    elif choice == "3":
        choice_com = random.randint(1, 3)
        print(choice_com)

        if choice_com == 1:
            print("You lost!")
            score_com = score_com + 1
        if choice_com == 2:
            print("You won!")
            score = score + 1
        if choice_com == 3:
            print("Tie!")   
            
    
#the order of how things are supposed to be displayed and a simple way of seeing the score count
start = input("Start? y/n")
if start == "y":
    game1()
    print("Score player 1: ")
    print(score)
    print("Score player 2: ")
    print(score_com)
    game1()
    print("Score player 1: ")
    print(score)
    print("Score player 2: ")
    print(score_com)
    if score == 2:
        print("Player 1 wins!")
        exit()
    if score_com == 2:
        print("Player 2 wins!") 
        exit()  
    game1()
    print("Score player 1: ")
    print(score)
    print("Score player 2: ")
    print(score_com)
    if score == 3:
        print("Player 1 wins!")
    if score_com == 3:
        print("Player 2 wins!")
    if score == 2:
        print("Player 1 wins!")
    if score_com == 2:
        print("Player 2 wins!")    

The issue is probably a pretty simple fix but as I said I'm new to programming so I cant find a fix. Thanks in advance to anyone that helps me!


Solution

  • A simple fix would be adding the following lines at the beginning of your function so that it uses the global variables

    def game1():
        global score
        global score_com
    
        ...
    

    Or, you can have game1 take both scores variable and return the updated one.