不断收到全局名称未定义错误
def defineType(riskCode):
if riskCode == 1:
riskType = High
else:
if riskCode == 2:
riskType = Moderate
else:
if riskCode == 3:
riskType = Low
else:
if RiskCode == 4:
riskType = No
这是我正在为一个班级编写的程序的一部分...
# Global Variables
custName = input('Please enter your name: ')
custAge = int(input('Please enter your age: '))
custViolation = int(input('Please enter the number of violations: '))
riskCode = 0
estimatePrice = 0
riskType = none
这些是我的全局变量...
Traceback (most recent call last):
File "C:\Users\Alli\Downloads\HwyRobbery.py", line 13, in <module>
riskType = none
NameError: name 'none' is not defined
这是我不断收到的错误,具体取决于我为尝试解决问题所做的
更改任何帮助将不胜感激!
谢谢! 阿里
def defineType(riskCode):
if riskCode == 1:
riskType = High
else:
if riskCode == 2:
riskType = Moderate
else:
if riskCode == 3:
riskType = Low
else:
if RiskCode == 4:
riskType = No
This is part of a program I'm working on for a class...
# Global Variables
custName = input('Please enter your name: ')
custAge = int(input('Please enter your age: '))
custViolation = int(input('Please enter the number of violations: '))
riskCode = 0
estimatePrice = 0
riskType = none
These are my global variable...
Traceback (most recent call last):
File "C:\Users\Alli\Downloads\HwyRobbery.py", line 13, in <module>
riskType = none
NameError: name 'none' is not defined
This is the error that I keep getting with different variations depending on what changes I make to try to solve the problem
Any help would be greatly appreciated!!!
Thanks!
Alli
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它是
无
而不是无
。 Python 区分大小写。It's
None
notnone
. Python is case sensitive.我看到的最初问题是
none
不是 Python 中的内置值。不过,无
。此外,第一个函数中的嵌套 if 语句塔是不必要的。把它压扁!
这不是一个错误,但它更具可读性,不是吗? (也更容易打字:)
另外,我认为
高
、中等
、低
和否
是在代码中的其他地方定义?如果您将它们用作枚举常量(即作为一组其值不会更改的唯一整数),则HIGH
、MODERATE
、LOW
,并且NO
会更惯用。事实上,如果你这样做了,整个函数就可以像这样被取消:
现在你根本不需要映射它们;您可以将
riskCode
分配给riskType
,尽管现在这可能有点多余!如果您想从风险代码中获取字符串,请尝试第二个字典 摘自 Artsiom Rudzenka 的回答。
最后...我建议将这些代码放在它们自己的命名空间中:
然后您可以像这样引用它们:
现在您可以自由地使用诸如
NO
之类的简短单词来表示其他内容。The initial problem I see is that
none
is not a built-in value in Python.None
is though.Also, the tower of nested if statements in the first function is unnecessary. Flatten that!
It's not an error, but it's much more readable, no? (Easier to type too :)
Also, I take it that
High
,Moderate
,Low
, andNo
are defined elsewhere in your code? If you're using them as enumerated constants (i.e. as a set of unique integers whose values do not change),HIGH
,MODERATE
,LOW
, andNO
would be more idiomatic.And in fact, if you did that, the whole function could be done away with like so:
Now you don't need to map them at all; and you can just assign
riskCode
toriskType
, although that may be a bit redundant now!If you want to get strings from risk codes, try the second dictionary from Artsiom Rudzenka's answer.
Finally... I would suggest putting these codes in their own namespace:
You can then reference them like so:
Now you are free to use short words like
NO
for other things.无需使用函数来使用函数进行风险映射,您可以简单地使用以下内容:
但如果 High, ...No 不是实例或变量,而只是字符串,则:
用法:
或者
No need to use function to use function for risks mapping you can simply use the following:
But in case if High, ...No is not a instances or variables, but just a strings than:
Usage:
Or
除此之外,我对简单性的贡献是:
Apart from none, my contribution to simplicity:
如果您要修改函数中的全局变量而不是声明它的变量,则不要这样做
你必须使用 global 关键字。
If you are modifying global variables in a function other than were it is declared don't
you have to use the global keyword.
在函数内部设置
riskType
是该函数的本地值,不会影响全局值。您需要通过将globalriskType
放在尝试修改它的代码之前来告诉您的函数使用全局。None
应该大写。我相信您的意思是将riskType 设置为字符串,因此应该引用这些值。
Python 有一个
elif
来表示else if
。更正后的代码:
Setting
riskType
inside your function is local to that function and will not affect the value in the global. You need to tell your function to use the global by puttingglobal riskType
before code that tries to modify it.None
should be capitalized.I believe you meant to set riskType to a string and therefore those values should be quoted.
Python has an
elif
forelse if
.The corrected code: