function - python return - "name not defined" -


i'm trying script write information basic text file. i'm still learning python.

the problem lies here:

file_text = """%s shoot details\n number of shoots:\t %s maximum range:\t %s number of firers:\t %s number of lanes active:\t %s number of details:\t %s troops per detail:\t %s \n  range staff ammo nco:\t %s medical supervisor:\t %s ic butts:\t %s""" % (shoot_name, number_of_shoots, max_range, number_of_firers, number_of_lanes, number_of_details, size_of_details, ammo_nco, medical_supervisor, ic_butts) 

the error message: nameerror: name 'number_of_details' not defined

the above (supposedly) written file this:

def generatedocument():     file = open(file_name, 'w')     file.write(file_text)     os.startfile(file_name) 

however did define earlier in following function:

def detailformation():     number_of_details = number_of_firers / number_of_lanes     return number_of_details 

identical problems occur size_of_details, defined:

def detailsizer():     size_of_details = number_of_firers / detailformation()     return size_of_details 

and range staff chooser function, gives me similar error. come function randomly selects them list:

def range_staff():     print "\n"     ammo_nco = (random.choice(fiveplatoon))     print "ammo nco: "+ammo_nco     fiveplatoon.remove(ammo_nco)     medical_supervisor = (random.choice(fiveplatoon))     print "medical supervisor: "+medical_supervisor     fiveplatoon.remove(medical_supervisor)     ic_butts = (random.choice(fiveplatoon))     print "ic butts/console: "+ic_butts     fiveplatoon.remove(ic_butts)     return range_staff 

it seems i'm missing fundamental here. how can python recognise?

this looks scope issue. variables declared inside functions defined within function. fix it, put declaration of variable on same scope used. see below sample code

number_of_details = 0  def detailformation():     number_of_details = number_of_firers / number_of_lanes     return number_of_details  file_text = """%s shoot details\n number of shoots:\t %s maximum range:\t %s number of firers:\t %s number of lanes active:\t %s number of details:\t %s troops per detail:\t %s \n  range staff ammo nco:\t %s medical supervisor:\t %s ic butts:\t %s""" % (shoot_name, number_of_shoots, max_range, number_of_firers, number_of_lanes, number_of_details, size_of_details, ammo_nco, medical_supervisor, ic_butts) 

note quick solution (putting variable on global scope).


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -