UnboundLocalError - code is short & simple

Discussion in 'Python' started by pylearner, Sep 28, 2009.

  1. pylearner

    pylearner Guest

    System Specs:

    Python version = 2.6.1
    IDLE
    Computer = Win-XP, SP2 (current with all windows updates)

    ----------------------------------------------------------------------------------------

    Greetings:

    I have written code for two things: 1) simulate a coin toss, and 2)
    assign the toss result to a winner. Code for the simulated coin toss
    is in a file named "coin_toss.py." Code for the assignment of the
    toss result is in a file named "toss_winner.py." Each file has one
    simple function: 1) coin_toss(), and 2) toss_winner(), respectively.
    (The code for each file is listed below.)


    Problem:

    I am getting an error when I run "toss_winner.py."


    Error Message:

    Traceback (most recent call last):
    File "<pyshell#2>", line 1, in <module>
    toss_winner()
    File "C:/Python26/toss_winner.py", line 7, in toss_winner
    coin_toss = coin_toss()
    UnboundLocalError: local variable 'coin_toss' referenced before
    assignment


    Question #1:

    After reviewing the code below, does anybody know why I am getting
    this error?


    Explanation:

    As I understand, the first statement of the toss_winner() function
    body -- i.e. "coin_toss = coin_toss()" -- causes four things to
    happen: 1) the coin_toss() function is called, 2) the coin_toss()
    function is executed, 3) the coin_toss() function returns the value of
    its local "coin_toss" variable, and 4) the returned value of the "coin
    toss" variable that is local to the coin_toss() function is assigned
    to the "coin toss" variable that is local to the toss_winner()
    function.

    Given this understanding, it seems I should NOT be getting a
    "referenced before assignment" error, involving the "coin_toss" local
    variable of "toss_winner()."


    Note:

    I am new to programming and Python. I'm currently self-studying
    "Python Programming: An Intro to Computer Science" by Zelle.

    Thanks!

    ---------------------------------------------------------------

    # toss_winner.py

    from coin_toss import coin_toss

    def toss_winner():

    coin_toss = coin_toss()

    if coin_toss == "Heads":
    toss_winner = "Player A"
    print 'From "toss_winner" function >>',
    print "Toss Winner = " + str(toss_winner)

    else:
    toss_winner = "Player B"
    print 'From "toss_winner" function >>',
    print "Toss Winner = " + str(toss_winner)

    return toss_winner

    ---------------------------------------------------------------

    # coin_toss.py

    from random import random

    def coin_toss():

    random_number = random()

    if random_number < .5:

    coin_toss = "Heads"

    print 'From "coin_toss" function >>',
    print "Toss result = " + str(coin_toss)

    else:

    coin_toss = "Tails"

    print 'From "coin_toss" function >>',
    print "Toss result = " + str(coin_toss)

    return coin_toss
     
    pylearner, Sep 28, 2009
    #1
    1. Advertising

  2. On Sep 28, 6:07 am, pylearner <> wrote:
    > System Specs:
    >
    > Python version = 2.6.1
    > IDLE
    > Computer = Win-XP, SP2 (current with all windows updates)
    >
    > ---------------------------------------------------------------------------­-------------
    >
    > Greetings:
    >
    > I have written code for two things:  1) simulate a coin toss, and 2)
    > assign the toss result to a winner.  Code for the simulated coin toss
    > is in a file named "coin_toss.py."  Code for the assignment of the
    > toss result is in a file named "toss_winner.py."  Each file has one
    > simple function:  1) coin_toss(), and 2) toss_winner(), respectively.
    > (The code for each file is listed below.)
    >
    > Problem:
    >
    > I am getting an error when I run "toss_winner.py."
    >
    > Error Message:
    >
    > Traceback (most recent call last):
    >   File "<pyshell#2>", line 1, in <module>
    >     toss_winner()
    >   File "C:/Python26/toss_winner.py", line 7, in toss_winner
    >     coin_toss = coin_toss()
    > UnboundLocalError: local variable 'coin_toss' referenced before
    > assignment
    >
    > Question #1:
    >
    > After reviewing the code below, does anybody know why I am getting
    > this error?
    >
    > Explanation:
    >
    > As I understand, the first statement of the toss_winner() function
    > body -- i.e. "coin_toss = coin_toss()" -- causes four things to
    > happen: 1) the coin_toss() function is called, 2) the coin_toss()
    > function is executed, 3) the coin_toss() function returns the value of
    > its local "coin_toss" variable, and 4) the returned value of the "coin
    > toss" variable that is local to the coin_toss() function is assigned
    > to the "coin toss" variable that is local to the toss_winner()
    > function.
    >
    > Given this understanding, it seems I should NOT be getting a
    > "referenced before assignment" error, involving the "coin_toss" local
    > variable of "toss_winner()."
    >
    > Note:
    >
    > I am new to programming and Python.  I'm currently self-studying
    > "Python Programming: An Intro to Computer Science" by Zelle.
    >
    > Thanks!
    >
    > ---------------------------------------------------------------
    >
    > # toss_winner.py
    >
    > from coin_toss import coin_toss
    >
    > def toss_winner():
    >
    >     coin_toss = coin_toss()
    >
    >     if coin_toss == "Heads":
    >         toss_winner = "Player A"
    >         print 'From "toss_winner" function >>',
    >         print "Toss Winner = " + str(toss_winner)
    >
    >     else:
    >         toss_winner = "Player B"
    >         print 'From "toss_winner" function >>',
    >         print "Toss Winner = " + str(toss_winner)
    >
    >     return toss_winner
    >
    > ---------------------------------------------------------------
    >
    > # coin_toss.py
    >
    > from random import random
    >
    > def coin_toss():
    >
    >     random_number = random()
    >
    >     if random_number < .5:
    >
    >         coin_toss = "Heads"
    >
    >         print 'From "coin_toss" function >>',
    >         print "Toss result = " + str(coin_toss)
    >
    >     else:
    >
    >         coin_toss = "Tails"
    >
    >         print 'From "coin_toss" function >>',
    >         print "Toss result = " + str(coin_toss)
    >
    >     return coin_toss


    You should not use the same name (e.g. coin_toss ) for the function
    and the variable. Change one of the two, and things will go better.

    Ciao
    ------
    FB
     
    Francesco Bochicchio, Sep 28, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Geering

    longs, long longs, short short long ints . . . huh?!

    David Geering, Jan 8, 2007, in forum: C Programming
    Replies:
    15
    Views:
    565
    Keith Thompson
    Jan 11, 2007
  2. Replies:
    4
    Views:
    831
    Kaz Kylheku
    Oct 17, 2006
  3. pylearner
    Replies:
    2
    Views:
    267
    Bruno Desthuilliers
    Sep 28, 2009
  4. Rich Healey
    Replies:
    4
    Views:
    318
    Albert Hopkins
    Sep 30, 2009
  5. Replies:
    9
    Views:
    100
    John Gordon
    Jun 24, 2013
Loading...

Share This Page