[Tutor] An unknown error in my program

daedae11 daedae11 at 126.com
Sun Dec 25 11:18:50 CET 2011


The following program has an error : 
                                          new += lists[int(j)]+"-";
UnboundLocalError: local variable 'new' referenced before assignment

But when I put the sentence  " new = '' " in the main() function, the program run normally.

Please tell me why?  Isn't variable new in the following program is a global variable?

the program is:
lists = ['zero','one','two','three','four','five','six','seven','eight','nine'];
new = "";


def main():

    while True:
        try:
            iput = int(raw_input('Please input a int(0~1000): '))
            if not 0<=iput<=1000:
                continue
        except:
            continue
        break

    iput = str(iput)
    for j in iput:
        new += lists[int(j)]+"-";
    print new[0:-1];

if __name__ == "__main__":
    main();




daedae11
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111225/43214408/attachment.html>


More information about the Tutor mailing list