Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: A Python question

  1. #1
    Join Date
    Jul 2009
    Location
    Québec, Canada
    Beans
    51
    Distro
    Ubuntu 11.04 Natty Narwhal

    Red face A Python question

    Hi everyone,

    I define a little function in Python like this:

    Code:
    def f(t, T):
        if 0 < t < T/2.0:
            value = 1.0
        elif t == T/2.0:
            value = 0.0
        elif T/2.0 < t < float(T):
            value = -1.0
        return value
    And when i run this in IPython, I obtain the following error:

    HTML Code:
    ---------------------------------------------------------------------------
    UnboundLocalError                         Traceback (most recent call last)
    
    /home/cppbuntu/Desktop/Python/Problems/Chapter 02/<ipython console> in <module>()
    
    /home/cppbuntu/Desktop/Python/Problems/Chapter 02/<ipython console> in f(t, T)
    
    UnboundLocalError: local variable 'value' referenced before assignment
    I don't understand...how can the variable 'value' be referenced before assignment in this code?

    Thanks for the help.

  2. #2
    Join Date
    Feb 2006
    Location
    Moshi, Tanzania
    Beans
    805
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: A Python question

    Ah, classical mistake

    Simply put, if the code passes in no branch, you ask python to return "value" when it has not been defined.

    Probably your second "elif" statement should be an "else" statement (so as to catch all cases), or even better, you should declare your "value" variable before entering the conditional part of the code (i.e. before "if").
    Assign it to be "None" if you can't think of any sensible default value.

    Here's your code, corrected:

    Code:
    def f(t, T):
        value = 0
        if 0 < t < T/2.0:
            value = 1.0
        elif t == T/2.0:
            value = 0.0
        elif T/2.0 < t < float(T): #Maybe if could simply be "else:"?
            value = -1.0
    
        return value
    Last edited by tribaal; November 16th, 2009 at 10:37 PM.

  3. #3
    Join Date
    Dec 2005
    Location
    The Netherlands
    Beans
    682
    Distro
    Ubuntu Development Release

    Re: A Python question

    When variables are assigned inside a function, they are always bound to the function's local namespace. The global namespace is never checked for 'value', hence the error. Your if's misses an else to catch all branches of the if.

    damn I lost to tribaal
    Last edited by Rinzwind; November 16th, 2009 at 10:36 PM.
    Hello, all my posts come without warranty

  4. #4
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: A Python question

    PHP Code:
    elif t == T/2.0
    Aside from what has been said above, you should NEVER compare floats directly, not in python, not in C/C++, not in Java, never (int and decimal are fine).

    Why? Because of the specification of floating point by the IEEE, floats are never accurate, as simple as doing:

    PHP Code:
    >>> 1.1 2.0
    3.1000000000000001 
    Leads to unexpected errors, floats must be used inside ranges.

    *edit:
    If you care for precision, then use the Decimal module (Fixed point). But it seems that you're working on things related to physics, so stick with float, but test within ranges as I told you before, as computational error are minimal compared to real life measurement errors (unless you count going out of range, overflows, etc)
    Last edited by Can+~; November 16th, 2009 at 11:25 PM. Reason: Added fixed point
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  5. #5
    Join Date
    Jul 2009
    Location
    Québec, Canada
    Beans
    51
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: A Python question

    So many thanks to all of you! Good explanations! Very appreciated.

  6. #6
    Join Date
    Jul 2009
    Location
    Québec, Canada
    Beans
    51
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: A Python question

    So, you test for what?
    elif abs(t - T/2.0) < 1e-6:
    <statements>
    doesn't work either...i need that my function f(t, 2) return the value 0.0 when t = 1.0...

  7. #7
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: A Python question

    PHP Code:
    def feq(abepsylon=0.000001):
        return 
    abs(b) < epsylon 
    So now:

    PHP Code:
    feq(0.10.10000001# yields true 
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  8. #8
    Join Date
    Jul 2009
    Location
    Québec, Canada
    Beans
    51
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: A Python question

    Yeah, that's exactly what i've done...

    Code:
    def f(t, T):
        if 0 < t < T/2.0:
            value = 1.0
        elif T/2.0 < t < float(T):
            value = -1.0
        elif abs(t - T/2.0) < 1e-6:
            value = 0.0
        else:
            value = 0.0
        return value
    
    # i know that i can use the numpy module...
    # but for this problem, it's supposed that we don't know yet ;)
    def makelist(start, stop, inc):
        val = start
        tmp   = []
        while val <= (stop + inc):
            tmp.append(val)
            val += inc
        return tmp
        
    for t in makelist(0, 2.5, 0.1):
        print '%5.2f\t%5.2f' % (t, f(t, 2))
    The result of this is

    HTML Code:
     0.00     0.00
     0.10     1.00
     0.20     1.00
     0.30     1.00
     0.40     1.00
     0.50     1.00
     0.60     1.00
     0.70     1.00
     0.80     1.00
     0.90     1.00
     1.00     1.00 # supposed to be 0.0!
     1.10    -1.00
     1.20    -1.00
     1.30    -1.00
     1.40    -1.00
     1.50    -1.00
     1.60    -1.00
     1.70    -1.00
     1.80    -1.00
     1.90    -1.00
     2.00     0.00
     2.10     0.00
     2.20     0.00
     2.30     0.00
     2.40     0.00
     2.50     0.00
    What i'm missing?

  9. #9
    Join Date
    Feb 2008
    Location
    readlink("/proc/self/exe"
    Beans
    1,120
    Distro
    Ubuntu Development Release

    Wink Re: A Python question

    C++ has a definition for the machine epsilon, you might use that one.
    In a world without walls and fences, who needs Windows and Gates?
    Linux is like a wigwam.... no Gates, no Windows but Apache inside!
    http://www.debianadmin.com
    apt-get install libstdc++6-4.3-doc

  10. #10
    Join Date
    May 2006
    Beans
    1,790

    Re: A Python question

    Quote Originally Posted by C++buntu View Post
    Yeah, that's exactly what i've done...

    Code:
    def f(t, T):
        if 0 < t < T/2.0:
            value = 1.0
        elif T/2.0 < t < float(T):
            value = -1.0
        elif abs(t - T/2.0) < 1e-6:
            value = 0.0
        else:
            value = 0.0
        return value
    
    # i know that i can use the numpy module...
    # but for this problem, it's supposed that we don't know yet ;)
    def makelist(start, stop, inc):
        val = start
        tmp   = []
        while val <= (stop + inc):
            tmp.append(val)
            val += inc
        return tmp
        
    for t in makelist(0, 2.5, 0.1):
        print '%5.2f\t%5.2f' % (t, f(t, 2))
    The result of this is

    HTML Code:
     0.00     0.00
     0.10     1.00
     0.20     1.00
     0.30     1.00
     0.40     1.00
     0.50     1.00
     0.60     1.00
     0.70     1.00
     0.80     1.00
     0.90     1.00
     1.00     1.00 # supposed to be 0.0!
     1.10    -1.00
     1.20    -1.00
     1.30    -1.00
     1.40    -1.00
     1.50    -1.00
     1.60    -1.00
     1.70    -1.00
     1.80    -1.00
     1.90    -1.00
     2.00     0.00
     2.10     0.00
     2.20     0.00
     2.30     0.00
     2.40     0.00
     2.50     0.00
    What i'm missing?
    Put a print statement in each branch of f and see which path the execution takes.

    (Was this solved? Is it unsolved again?)

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •