python return - “name not defined”

2020-05-06 13:45发布

问题:

I'm trying to get this script to write information into a 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' is not defined

The above is (supposedly) written into a file with this:

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

However I did define it earlier in the following function:

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

Identical problems occur for the size_of_details, defined:

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

And also with the range staff chooser function, sometimes it gives me a similar error. They come from this function that randomly selects them from a 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 like I'm missing something fundamental here. How can I get python to recognise?

回答1:

This looks like a scope issue. Variables declared inside functions are only defined within that function. To fix it, put the declaration of the variable on the same scope as where it is used. See below for the 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 that this is just a quick solution (putting the variable on the global scope).



回答2:

The variables only exist in the scope you declare them

def fun():
    x = 5
    return x

>>> x
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    x
NameError: name 'x' is not defined

If you want to use the return value from a function, then call the function and assign it to such a variable

>>> x = fun()
>>> x
5


回答3:

Call your functions in order to get the value they return

size_of_details = detailsizer()