Ryan Broman
Ryan Broman

Reputation: 35

"Variable ___ referenced before assignment."

I've been having a bit of difficulty with my Python programme. It plays Rock Paper Scissors. The code is pretty large so I'll shorten it to a sample programme, but the full code can be viewed at https://github.com/Ph0enix0/doitall/blob/master/computerpungramming/rockpaperscissors.py

(count is the amount of games played)

count = 0
user_wins = 0
comp_wins = 0

def game():

    comp_answer = ['rock', 'paper', 'scissors']
    user_answer = input('Rock, paper, or scissors?')

    if user_answer = 'rock':
        count += 1

        if comp_answer = 'paper':
            print('Paper! I win!)
            comp_wins += 1
#The rest of the comparisons are here
while 1 == 1:
game()

If I put the variables in game() they will reset every time. Help?

Upvotes: 0

Views: 98

Answers (3)

dmilliken
dmilliken

Reputation: 1

You're going to want to change

if comp_answer = 'paper':

to

if comp_answer == 'paper':

That's probably what's throwing the error. Change the = to == in any other places where you're checking an if condition.

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180411

You need to move the variables inside the function and define scissors_wins:

def game():
    '''
    The actual game code. The code randomly chooses
    rock, paper, or scissors. Then, it checks if the user's choice
    beats the computer's. If it does, the user wins, and vice versa.
    '''

    #User picks rock, paper, or scissors.
    user_answer = input('Rock, paper, or scissors? ')


    #Tell the code that the game is not finished. Used for while loops.
    game_finished = False
    count = 0
    user_wins = 0
    comp_wins = 0
    scissors_wins = 0

Your game already has a loop so unless you are doing something in your while 1 == 1 then simply call game()

And yes every time you call the function the variables will be reset to 0.

If you want the win count to persist through many runs use a dict declaring outside the function:

d = {"user":0,"comp":0,"scissor":0}

Then update at the end of each game:

 d["user"] += user_wins 

Or increase the count of each inside the function instead of using the variables.

You also don't seem to ever break the loop so your code will loop infinitely. I imagine you want to also ask the user more than once for input so move user_answer = input('Rock, paper, or scissors? ') inside the loop.

Something like the following:

d = {"user":0,"comp":0,"scissor":0}

d = {"user":0,"comp":0,"scissor":0}

def game():
    '''
    The actual game code. The code randomly chooses
    rock, paper, or scissors. Then, it checks if the user's choice
    beats the computer's. If it does, the user wins, and vice versa.
    '''  

    #Tell the code that the game is not finished. Used for while loops.
    game_finished = False
    count = 0
    user_wins = 0
    comp_wins = 0
    scissors_wins = 0

    #Game isn't finished, so the code continues.
    while not game_finished:
            user_answer = input('Rock, paper, or scissors? ')

   # update dict with totals when loop breaks

Upvotes: 2

TigerhawkT3
TigerhawkT3

Reputation: 49318

The game function can't change the variables outside it (count, user_wins, comp_wins). Have the function return the necessary values.

count = 0
user_wins = 0
comp_wins = 0
def game():
    ...
    return count, user_wins, comp_wins
count, user_wins, comp_wins = game()

Upvotes: 1

Related Questions