Python – "Local x variable referenced before assignment"

Question:

I’m having a problem with my python program and I’ve spent too many time trying to fix it but I can’t. I was hoping you could help me.

Anyway, the problem is in:

def choose_winnerPvsP(p1,p2):

When I run it the part of Player vs Computer woks perfectly, but the part of Player vs Player gives me this error: “Local variable player_1score referenced before assignment”
I can’t understand why the if’s are never checked.

PS: I’m a beginner and I know that the code could be more compact, but for now I just want this thing working. Thanks in advance

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from random import *
from time import sleep
import sys
import os

os.system("cls") #ou "clear" se estiver em linux

print "******Welcome to Rock Paper Scissors Game!******n"

playerOrComputer = str(raw_input("Would you like to play against a friend or a computer?(F or C) n" ))

p1_score = 0
p2_score = 0

def player_turn(nome_player):
    print "What do you want to do, " + nome_player + "?"
    pchoice = str(raw_input("ROCK (R) PAPER (P) SCISSORS (S) or Quitn>> "))
    if pchoice == "R" or pchoice == "r":
        print "You chosed ROCK"
    elif pchoice == "P" or pchoice == "p":
        print "You chosed PAPER"
    elif pchoice == "S" or pchoice == "s":
        print "You chosed SCISSORS"
    elif pchoice == "quit" or pchoice == "Quit":
        sys.exit()
    else:
        print "I didn't understand! Please repeat."
        player_turn()
    return pchoice

def player_turn2p(nome_player1,p2):
    print "What do you want to do, " + nome_player1 + "?"
    pchoice1 = str(raw_input("ROCK (R) PAPER (P) SCISSORS (S)n>> "))
    if pchoice1 == "R" or pchoice1 == "r":
        pchoice1 = "ROCK"
        print "You chosed ROCK"
        sleep(1)
        os.system("cls")
    elif pchoice1 == "P" or pchoice1 == "p":
        pchoice1 = "PAPER"
        print "You chosed PAPER"
        sleep(1)
        os.system("cls")
    elif pchoice1 == "S" or pchoice1 == "s":
        pchoice1 = "SCISSORS"
        print "You chosed SCISSORS" 
        sleep(1)
        os.system("cls")
    elif pchoice1 == "quit" or pchoice == "Quit":
        sys.exit()
    else:
        print "I didn't understand!"
        player_turn2p(nome_player1,p2)
    print "What do you want to do, " + p2 + "?"
    pchoice2 = str(raw_input("ROCK (R) PAPER (P) SCISSORS (S)n>> "))
    if pchoice2 == "R" or pchoice2 == "r":
        print p2 + " chosed ROCKn" + nome_player1 + " chosed " + pchoice1
    elif pchoice2 == "P" or pchoice2 == "p":
        print p2 + " chosed PAPERn" + nome_player1 + " chosed " + pchoice1
    elif pchoice2 == "S" or pchoice2 == "s":
        print p2 + " chosed SCISSORSn" + nome_player1 + " chosed " + pchoice1
    elif pchoice1 == "quit" or pchoice == "Quit":
        sys.exit()
    else:
        print "I didn't understand!"
        player_turn2p(nome_player1,p2)
    return pchoice1, pchoice2

def computer_turn():
    choicecomp = randint(1,3)
    if choicecomp == 1:
        choicecomp = "ROCK"
    elif choicecomp == 2:
        choicecomp = "PAPER"
    elif choicecomp == 3:
        choicecomp = "SCISSORS"
    return choicecomp
    #1-Rock 2-Paper 3-Scissors

def choose_winnerPvsP(p1,p2):
    player_choice1, player_choice2= player_turn2p(p1,p2)
    if player_choice1 == "R" and player_choice2 == "R" or player_choice1 == "r" and player_choice2 == "r" or player_choice1 == "R" and player_choice2 == "r" or player_choice1 == "r" and player_choice2 == "R":
        player_1score = "lose"
        player_2score = "win"
        print "******It's a draw!******"
    elif player_choice1 == "R" and player_choice2 == "P" or player_choice1 == "r" and player_choice2 == "p" or player_choice1 == "R" and player_choice2 == "p" or player_choice1 == "r" and player_choice2 == "P":
        player_1score = "lose"
        player_2score = "win"
        print "******" + p2 + " wins!******"
    elif player_choice1 == "R" and player_choice2 == "S" or player_choice1 == "r" and player_choice2 == "s" or player_choice1 == "R" and player_choice2 == "s" or player_choice1 == "r" and player_choice2 == "S":
        player_1score = "win"
        player_2score = "lose"
        print "******" + p1 + " wins!******"
    elif player_choice1 == "P" and player_choice2 == "R" or player_choice1 == "p" and player_choice2 == "r" or player_choice1 == "P" and player_choice2 == "r" or player_choice1 == "p" and player_choice2 == "R":
        player_1score = "win"
        player_2score = "lose"
        print "******" + p1 + " wins!******"
    elif player_choice1 == "P" and player_choice2 == "P" or player_choice1 == "p" and player_choice2 == "p" or player_choice1 == "P" and player_choice2 == "p" or player_choice1 == "p" and player_choice2 == "P":    
        player_1score = "draw"
        player_2score = "draw"
        print "******It's a draw!******"
    elif player_choice1 == "P" and player_choice2 == "S" or player_choice1 == "p" and player_choice2 == "s" or player_choice1 == "P" and player_choice2 == "s" or player_choice1 == "p" and player_choice2 == "S":
        player_1score = "lose"
        player_2score = "win"
        print "******" + p2 + " wins!******"
    elif player_choice1 == "S" and player_choice2 == "R" or player_choice1 == "s" and player_choice2 == "r" or player_choice1 == "S" and player_choice2 == "r" or player_choice1 == "s" and player_choice2 == "R":
        player_1score = "lose"
        player_2score = "win"
        print "******" + p2 + " wins!******"
    elif player_choice1 == "S" and player_choice2 == "P" or player_choice1 == "s" and player_choice2 == "p" or player_choice1 == "S" and player_choice2 == "p" or player_choice1 == "s" and player_choice2 == "P":
        player_1score = "win"
        player_2score = "lose"
        print "******" + p1 + " wins!******"
    elif player_choice1 == "S" and player_choice2 == "S" or player_choice1 == "s" and player_choice2 == "s" or player_choice1 == "S" and player_choice2 == "s" or player_choice1 == "s" and player_choice2 == "S":
        player_1score = "draw"
        player_2score = "draw"
        print "******It's a draw!******"
    return player_1score, player_2score

def choose_winnerPvsC(player_name,player_choice, computer_choice):

    if player_choice == "R" and computer_choice == "ROCK" or player_choice == "r" and computer_choice == "ROCK":
        computer_score = "draw"
        player_score = "draw"
        print "******It's a draw!****** "
    elif player_choice == "R" and computer_choice == "PAPER" or player_choice == "r" and computer_choice == "PAPER":
        computer_score = "win"
        player_score = "lose"
        print "******I win!****** " 
    elif player_choice == "R" and computer_choice == "SCISSORS" or player_choice == "r" and computer_choice == "SCISSORS":
        player_score = "win"
        computer_score = "lose"
        print "******" + player_name + " wins!****** "
    elif player_choice == "P" and computer_choice == "ROCK" or player_choice == "p" and computer_choice == "ROCK":
        player_score = "win"
        computer_score = "lose"
        print "******" + player_name + " wins!****** "
    elif player_choice == "P" and computer_choice == "PAPER" or player_choice == "p" and computer_choice == "PAPER":    
        computer_score = "draw"
        player_score = "draw"
        print "******It's a draw!******"
    elif player_choice == "P" and computer_choice == "SCISSORS" or player_choice == "p" and computer_choice == "SCISSORS":
        computer_score = "win"
        player_score = "lose"
        print "******I win!****** "
    elif player_choice == "S" and computer_choice == "ROCK" or player_choice == "s" and computer_choice == "ROCK":
        computer_score = "win"
        player_score = "lose"
        print "******I win!****** "
    elif player_choice == "S" and computer_choice == "PAPER" or player_choice == "s" and computer_choice == "PAPER":
        player_score = "win"
        computer_score = "lose"
        print "******" + player_name + " wins!****** "
    elif player_choice == "S" and computer_choice == "SCISSORS" or player_choice == "s" and computer_choice == "SCISSORS":
        computer_score = "draw"
        player_score = "draw"
        print "******It's a draw!****** "
    return player_score, computer_score

def loopPvsP():
    p1score = 0           
    p2score = 0       
    while True:
        player_1score, player_2score = choose_winnerPvsP(p1,p2)
        if player_1score == "win":
            p1score += 1
        elif player_2score == "win":
            p2score += 1
        print p1 + ":",p1score
        print p2 + ":",p2score
        if p1score == 5:
            print "-+-+-+-+-"+ p1.upper() + "IS THE BIG WINNER!!-+-+-+-+-"
            break
        elif p2score == 5:
            print "-+-+-+-+-"+ p2.upper() + "IS THE BIG WINNER!!-+-+-+-+-"
            break       

def loopPvsC():
    pscore = 0
    cscore = 0
    while True:
        pchoice = player_turn(p1c)
        choicecomp = computer_turn()
        print "I chosed "+ choicecomp + "!"
        player_score, computer_score = choose_winnerPvsC(p1c,pchoice, choicecomp)
        if computer_score == "win":
            cscore += 1
        elif player_score == "win":
            pscore += 1
        print "Computer:",cscore
        print p1c + ":",pscore
        if pscore == 5:
            print "-+-+-+-+-YOU ARE THE BIG WINNER!!-+-+-+-+-"
            break
        elif cscore == 5:
            print "-+-+-+-+-I'M THE BIG WINNER!!-+-+-+-+-"
            break

if playerOrComputer == "f" or playerOrComputer == "F":
    p1 = str(raw_input("Player 1: "))
    p2 = str(raw_input("Player 2: "))
    os.system("cls")
    loopPvsP()

elif playerOrComputer == "c" or playerOrComputer == "C":
    p1c = str(raw_input("Player: "))
    os.system("cls")
    loopPvsC()

else:
    print "That's not what I asked!"
Asked By: João Pinhal

||

Answers:

Meh. Debuggers are overrated — I go years without using one in Python. Just add a final else clause to your big if/elif statement:

else:
    print "Did not find answer"
    print repr(player_choice), repr(computer_choice)

If that doesn’t hit, then it means you have a typo somewhere else, perhaps. Anyway, you can certainly add more debugging statements — one to each elif clause, if need be.

Which brings up another point — you have a lot of if/elif clauses.

You might want to investigate things like dicts with values you can add together, or functions you can call, to reduce the number of if/else statements you need.

Answered By: Patrick Maupin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.