Jump to content
  • Advertisement
Sign in to follow this  
koka282

UnboundLocalError: local variable 'comp' referenced before assignment

This topic is 1511 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Edited by koka282

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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")

Edited by KnolanCross

Share this post


Link to post
Share on other sites

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

Edited by koka282

Share this post


Link to post
Share on other sites

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

Edited by KnolanCross

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!