python返回 - “名称未定义” [英] python return - "name not defined"

查看:126
本文介绍了python返回 - “名称未定义”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让这个脚本将信息写入基本的文本文件。



 file_text =%s拍摄细节\ $ 
拍摄次数:\%s
最大范围:\%s
消防员人数:\% s
活跃车道数:\ t%s
详情数量:\%s
部队详情:\%s
\
范围员工
弹药NCO:\ t%s
医疗主管:\%s
IC屁股:\%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)

错误消息:
NameError:name'number_of_details'未定义



以上(据推测)写入文件中:

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

然而,我在前面的函数中定义了它:

  def detailformation():
number_of_details = number_of_firers / number_of_lanes
返回number_of_details

定义的size_of_details会出现相同的问题:

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

另外,对于范围员工选择器功能,有时也会给我类似的错误。它们来自这个从列表中随机选择它们的函数:

  def range_staff():
print\ n
ammo_nco =(random.choice(fiveplatoon))
printAmmo NCO:+ ammo_nco
fiveplatoon.remove(ammo_nco)
medical_supervisor =(random.choice(fiveplatoon ))
printMedical supervisor:+ medical_supervisor
fiveplatoon.remove(medical_supervisor)
ic_butts =(random.choice(fiveplatoon))
printIC Butts / Console: + ic_butts
fiveplatoon.remove(ic_butts)
return range_staff

好像是我在这里错过了一些基本的东西我怎样才能让python识别?

解决方案

这看起来像一个范围问题。在函数中声明的变量只在该函数中定义。为了解决这个问题,把变量的声明放在与作用域相同的地方。请参阅下面的示例代码

  number_of_details = 0 

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

file_text =%s拍摄细节\\\

拍摄次数:\%s
最大范围:\ t%s
消防队员数量:\ t%s
活跃车道数量:\ t%s
详情数量:\%s
部队每个细节:\%s
\
范围员工
弹药NCO:\ t%s
医疗主管:\%s
IC Butts:\ t%s%(shoot_name,number_of_fire,number_of_lanes,number_of_details,size_of_details,ammo_nco,medical_supervisor,ic_butts)

请注意,这只是一个快速解决方案(将变量放在全局范围内)。

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?

解决方案

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).

这篇关于python返回 - “名称未定义”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆