不断收到全局名称未定义错误

发布于 2024-11-15 17:21:30 字数 899 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

可爱暴击 2024-11-22 17:21:30

它是而不是。 Python 区分大小写。

It's None not none. Python is case sensitive.

罗罗贝儿 2024-11-22 17:21:30

我看到的最初问题是 none 不是 Python 中的内置值。不过,

此外,第一个函数中的嵌套 if 语句塔是不必要的。把它压扁!

def defineType(riskCode):
    if riskCode == 1:
        riskType = High
    elif riskCode == 2:
        riskType = Moderate
    elif riskCode == 3:
        riskType = Low
    elif RiskCode == 4:
        riskType = No

这不是一个错误,但它更具可读性,不是吗? (也更容易打字:)

另外,我认为中等是在代码中的其他地方定义?如果您将它们用作枚举常量(即作为一组其值不会更改的唯一整数),则 HIGHMODERATELOW ,并且 NO 会更惯用。

事实上,如果你这样做了,整个函数就可以像这样被取消:

HIGH = 1
MODERATE = 2
LOW = 3
NO = 4

现在你根本不需要映射它们;您可以将 riskCode 分配给 riskType,尽管现在这可能有点多余!

riskType = riskCode

如果您想从风险代码中获取字符串,请尝试第二个字典 摘自 Artsiom Rudzenka 的回答

最后...我建议将这些代码放在它们自己的命名空间中:

class Risk(object):
    HIGH = 1
    MODERATE = 2
    LOW = 3
    NO = 4

然后您可以像这样引用它们:

if riskCode == Risk.HIGH: do_something()

现在您可以自由地使用诸如 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!

def defineType(riskCode):
    if riskCode == 1:
        riskType = High
    elif riskCode == 2:
        riskType = Moderate
    elif riskCode == 3:
        riskType = Low
    elif RiskCode == 4:
        riskType = No

It's not an error, but it's much more readable, no? (Easier to type too :)

Also, I take it that High, Moderate, Low, and No 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, and NO would be more idiomatic.

And in fact, if you did that, the whole function could be done away with like so:

HIGH = 1
MODERATE = 2
LOW = 3
NO = 4

Now you don't need to map them at all; and you can just assign riskCode to riskType, although that may be a bit redundant now!

riskType = riskCode

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:

class Risk(object):
    HIGH = 1
    MODERATE = 2
    LOW = 3
    NO = 4

You can then reference them like so:

if riskCode == Risk.HIGH: do_something()

Now you are free to use short words like NO for other things.

安静被遗忘 2024-11-22 17:21:30

无需使用函数来使用函数进行风险映射,您可以简单地使用以下内容:

riskmapping = {1:High, 2:Moderate, 3:Low, 4:No}

但如果 High, ...No 不是实例或变量,而只是字符串,则:

riskmapping = {1:'High', 2:'Moderate', 3:'Low', 4:'No'}

用法:

highRisk = riskmapping[1]

或者

if riskmapping[currentRiskCode]:
    #do smth

No need to use function to use function for risks mapping you can simply use the following:

riskmapping = {1:High, 2:Moderate, 3:Low, 4:No}

But in case if High, ...No is not a instances or variables, but just a strings than:

riskmapping = {1:'High', 2:'Moderate', 3:'Low', 4:'No'}

Usage:

highRisk = riskmapping[1]

Or

if riskmapping[currentRiskCode]:
    #do smth
余生一个溪 2024-11-22 17:21:30

除此之外,我对简单性的贡献是:

riskCodes = {1: 'High', 2: 'Moderate', 3: 'Low', 4: 'No'}

def defineType(riskCode):
    riskType = riskCodes[riskCode]

Apart from none, my contribution to simplicity:

riskCodes = {1: 'High', 2: 'Moderate', 3: 'Low', 4: 'No'}

def defineType(riskCode):
    riskType = riskCodes[riskCode]
明媚殇 2024-11-22 17:21:30

如果您要修改函数中的全局变量而不是声明它的变量,则不要这样做
你必须使用 global 关键字。


def defineType(riskCode):
    global riskType

    if riskCode == 1:
        riskType = High



If you are modifying global variables in a function other than were it is declared don't
you have to use the global keyword.


def defineType(riskCode):
    global riskType

    if riskCode == 1:
        riskType = High



乖不如嘢 2024-11-22 17:21:30

在函数内部设置 riskType 是该函数的本地值,不会影响全局值。您需要通过将 globalriskType 放在尝试修改它的代码之前来告诉您的函数使用全局。

None 应该大写。

我相信您的意思是将riskType 设置为字符串,因此应该引用这些值。

Python 有一个 elif 来表示 else if

更正后的代码:

# Global Variables
riskCode = 0
estimatePrice = 0
riskType = None

def defineType(riskCode):
    global riskType
    if riskCode == 1:
        riskType = "High"
    elif riskCode == 2:
        riskType = "Moderate"
    elif riskCode == 3:
        riskType = "Low"
    elif RiskCode == 4:
        riskType = "No"

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 putting global 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 for else if.

The corrected code:

# Global Variables
riskCode = 0
estimatePrice = 0
riskType = None

def defineType(riskCode):
    global riskType
    if riskCode == 1:
        riskType = "High"
    elif riskCode == 2:
        riskType = "Moderate"
    elif riskCode == 3:
        riskType = "Low"
    elif RiskCode == 4:
        riskType = "No"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文