UnboundLocalError: local variable 'comp' referenced before assignment

Started by
3 comments, last by KnolanCross 9 years, 5 months ago

Hi,

i have little problem here

i was trying to make rock,paper,scissor project

while running it

i got this error UnboundLocalError: local variable 'comp' referenced before assignment

what should i do to fix it ?

code:


from random import randint

#Computer draws hand. 

def comp():
    r=randint(1,3)
    if r == 1:
        hand = "rock"
    if r == 2 :
        hand = "paper"
    if r == 3 :
        hand = "scissor"
    return hand

# who is the winner.


def whowin(you,comp):
    if you == comp:
        winner = "tie"
    elif comp == ("rock" and you == "scissor") or comp == ("scissor" and you == "paper") or comp == ("paper" and you == "rock"):
        winner = "comp"
    else:
        winner = "you"
    return winner    


# start

def play():
    print "------welcome to my game------"
    comp_wins = 0
    you_wins = 0
    tie_wins = 0
    while True:
        you = raw_input("Please choose rock or scissor or paper ")
        you=you.lower()
        comp = comp()
        winner = whowin(you,comp)
        if not you == "rock" or you == "paper" or you =="scissor":
            print" invalid"
            continue


play()        
        

Thanks smile.png

Advertisement
My Python is a bit sketchy but I would assume "comp = comp()" is a really bad idea. It creates a local variable 'comp' and then tries to initialize it by calling itself as a function in an uninitialized state.

1) You have a function with the same name of a variable, which is a very bad idea.

2) You should check the user input as soon as it is entered, not after you processed the round.

3) You can replace the second end third ifs of the comp function by if, elif and else.

4) The parenthesis of your whowin function are wrong, here is an example:

>>> comp = "rock"
>>> you = "scissor"
>>> comp == ("rock" and you == "scissor")
False

What python is interpreting in this code is:
"rock" == (bool("rock") and you == "scissor")
"rock" == (True and True)
"rock" == True
False

you probably meant:

(comp == "rock" and you == "scissor")

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).


2) You should check the user input as soon as it is entered, not after you processed the round.

Yes sad.png


You can replace the second end third ifs of the comp function by if, elif and else.

i must focus more i often type them by mistake


The parenthesis of your whowin function are wrong

it should be

elif (comp == "rock" and you == "scissor") or (comp == "scissor" and you == "paper") or (comp == "paper" and you == "rock")


You have a function with the same name of a variable, which is a very bad idea.

i want to make the comp be variable in whowin function & comp will be the hand value like : paper or rock ,,

i should type pc=comp() & whowin(you,pc)

you mean like that ?

Thanks

Smile smile.png

No problem.

pc=comp() & whowin(you,pc) will work.

But keep in mind that there is no need to save characters, you could rename the comp function to something like get_computer_hand.

Finally, there is a program called "pyflakes" that will check for errors, you could use it: https://pypi.python.org/pypi/pyflakes

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

This topic is closed to new replies.

Advertisement