Physics Forums

Physics Forums (http://www.physicsforums.com/index.php)
-   Programming & Computer Science (http://www.physicsforums.com/forumdisplay.php?f=165)
-   -   Error on Python (http://www.physicsforums.com/showthread.php?t=561543)

azerty12 Dec19-11 04:09 PM

Error on Python
 
Hi guys
I'm trying to program a Roulette game on Python
My programs asks for inputs and I gave names to these inputs.
The point is, since I didn't assign these names before, the following error occurs:
UnboundLocalError: local variable 'color' referenced before assignment

I tried to get rid of this error with exceptions, but I couldn't
Any idea?

radou Dec20-11 12:56 AM

Re: Error on Python
 
Could you paste your code here? Perhaps it's a problem of local/global variables? Do you refer to a variable which is defined inside a function?

azerty12 Dec21-11 06:07 AM

Re: Error on Python
 
1 Attachment(s)
Quote:

Quote by radou (Post 3676698)
Could you paste your code here? Perhaps it's a problem of local/global variables? Do you refer to a variable which is defined inside a function?

Hi Radou
Here's my program (edited with IDLE)
The problem occurs when I execute Roulette() on python's shell and when I choose 'No' when he asks me whether I want to play

I didn't put much comments on my program, so if there is something you don't understand, just ask.

Since I can't attach the file with IDLE, I'm gone send it to you on notepad

Mark44 Dec21-11 09:18 AM

Re: Error on Python
 
In your Game function, when the user responds with "No", the col variable is uninitialized.

Code:

def Game(bet,result):
    if result==51:
        print("first attempt") 
    else:
        print("You had to choose:",result)     
    print("You have:",bet,"$")
    if bet==0:
        return "plucked"
    play=input("do you want to play? Yes/No")
    if play=='No':
        print("Wise decision","you leave with",bet,"$")
    elif play=='Yes':
        gamble=input("What number do you choose?")
        gamble=int(gamble) #Number you bet on
        col=color(gamble)
    result=randrange(50)
   
    if result==gamble:
        return Game(3*bet,result)
    elif color(result)==col:
        return Game(ceil(0.5*bet),result)
    elif gamble>50:
        print("wrong choice")
    else:   
        return Game(0,result)

I believe that your problem occurs when the code tries to execute elif color(result) == col.

If this is the problem, you could fix it by assigning a value to col like this:
Code:

def Game(bet,result):
    col = 'Black'
    if result==51:
        print("first attempt") 
    else:
        print("You had to choose:",result)     
    print("You have:",bet,"$")
    if bet==0:
        return "plucked"
    play=input("do you want to play? Yes/No")
    if play=='No':
        print("Wise decision","you leave with",bet,"$")
    elif play=='Yes':
        gamble=input("What number do you choose?")
        gamble=int(gamble) #Number you bet on
        col=color(gamble)
    result=randrange(50)
   
    if result==gamble:
        return Game(3*bet,result)
    elif color(result)==col:
        return Game(ceil(0.5*bet),result)
    elif gamble>50:
        print("wrong choice")
    else:   
        return Game(0,result)

Another way that's probably better is to exit the function if the user enters "No".

azerty12 Dec21-11 09:32 AM

Re: Error on Python
 
I'm afraid the first way doesn't work, it generates the following exception:
UnboundLocalError: local variable 'col' referenced before assignment

About your second suggestion: Could you tell me how to exit the function please? (I just begging in programming)

Mark44 Dec22-11 09:08 AM

Re: Error on Python
 
Quote:

Quote by azerty12 (Post 3678569)
I'm afraid the first way doesn't work, it generates the following exception:
UnboundLocalError: local variable 'col' referenced before assignment

I didn't test the code I wrote, but I think it should work. Did you notice the line I added to your code?
Code:

def Game(bet,result):
    col = 'Black' <=== added

Quote:

Quote by azerty12 (Post 3678569)

About your second suggestion: Could you tell me how to exit the function please? (I just begging in programming)

Just return some appropriate value. BTW, there are several code paths in your Game function that don't return anything. Each of the if ... elif branches ought to return something.

azerty12 Dec22-11 09:31 AM

Re: Error on Python
 
Quote:

Quote by Mark44 (Post 3679981)
I didn't test the code I wrote, but I think it should work. Did you notice the line I added to your code?
Code:

def Game(bet,result):
    col = 'Black' <=== added


Yes I did notice the line you added. But it still raises the same kind of exception.


Quote:

Quote by Mark44 (Post 3679981)
Just return some appropriate value. BTW, there are several code paths in your Game function that don't return anything. Each of the if ... elif branches ought to return something.

Do you mean that instead of printing things like "wrong number"... I should return these?

Mark44 Dec22-11 01:27 PM

Re: Error on Python
 
Quote:

Quote by azerty12 (Post 3680002)
Yes I did notice the line you added. But it still raises the same kind of exception.

See if this makes a difference.
Code:

if play=='No':
        print("Wise decision","you leave with",bet,"$")
        return

As you have described things, you're getting the error when you type "No". In that case, col doesn't get set, but there is code below that executes, that tries to compare col with color(result).

I think that's what's causing your error.
Quote:

Quote by azerty12 (Post 3680002)



Do you mean that instead of printing things like "wrong number"... I should return these?


jhae2.718 Dec22-11 02:28 PM

Re: Error on Python
 
You have quite a few variables that are referenced when they may not be defined. Mark44's suggestion should take care of the problem.

azerty12 Dec25-11 09:48 AM

Re: Error on Python
 
Quote:

Quote by Mark44 (Post 3680296)
See if this makes a difference.
Code:

if play=='No':
        print("Wise decision","you leave with",bet,"$")
        return

As you have described things, you're getting the error when you type "No". In that case, col doesn't get set, but there is code below that executes, that tries to compare col with color(result).

I think that's what's causing your error.

Absolutely Mark, you got it!

Thanks a lot and thx to jihae and radou


All times are GMT -5. The time now is 06:52 PM.

Powered by vBulletin Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2014 Physics Forums