python - Can some one explain the use NameError in try except statements? -


i use nameerror in try except statement, 1 below:

from tkinter import *  # functions def chordlabeltextgen(chords, current_chord):     while true:         try:             return_value = chords             current_chord_pos = return_value.index(current_chord)             return_value.remove(current_chord_pos)             return return_value         except nameerror:             return_value = chords             return return_value  # main     root = tk() root.geometry("600x400") root.title("chord changes log") standard_tuning_chords = ["a","b","c","d","e","f","g"]  chord_names = chordlabeltextgen(standard_tuning_chords, current_chord) current_chord = chord_names[0] chord_names = chordlabeltextgen(standard_tuning_chords, current_chord) print (chord_names) 

but when run through idle returns error message:

traceback (most recent call last):   file "c:/users/jack/desktop/python projects/gutiar chord changes log prototype.py", line 26, in <module>     chord_names = chordlabeltextgen(standard_tuning_chords, current_chord) nameerror: name 'current_chord' not defined 

i thought expect statement would else statement , run second block instead, doesn't seem work way.
can explain me?

the problem nameerror exception raised in main , not in function. current_chord doesn't exist in scope when you're calling function, program fail before entering in function, when tried put arguments on stack...

if put this:

try:     chord_names = chordlabeltextgen(standard_tuning_chords, current_chord) except nameerror:     print("damn, there's error") 

... you'll see error message. besides, it's not beautiful handle undefined variables try/except blocks. should know when variable exists , when not.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -