全局名称没有定义?

时间:2014-03-25 13:34:47

标签: python-3.x

G'day研究员。我面临一个关于全局变量的问题,即使它是python声称的不是defiend。本质上,我只是想检查一个整数是否包含小数位,或者输入中不包含任何与之相关的整数。这是我的代码:

def Strength1():
    try:
        global strength1
        strength1 = int(input("%s, please enter your desired strength - between 1 and 20\n>"%name1))
        strength1int = int(strength1)
        def invLoop():
            clearScreen()
            Invalid()
            Strength1()
        if int(strength1) <= 0:
            invLoop()
        if int(strength1) >= 21:
            invLoop()
    except Exception as exception:
        clearScreen()
        print("'%s isn't an integer."%strength1)
        Strength1()
def Skill1():
    try:
        global skill1
        skill1 = int(input("%s, please enter your desired skill - between 1 and 20\n>"%name1))
        skill1int = int(skill1)
        def invLoop():
            clearScreen()
            Invalid()
            Skill1()
        if int(skill1) <= 0:
            invLoop()
        if int(skill1) >= 21:
            invLoop()
    except Exception as exception:
        clearScreen()
        print("'%s isn't an integer."%skill1)
        Skill1()
def Strength2():
    try:
        global strength2
        strength2 = int(input("%s, please enter your desired strength - between 1 and 20\n>"%name2))
        def invLoop():
            clearScreen()
            Invalid()
            Strength2()
        if int(strength2) <= 0:
            invLoop()
        if int(strength2) >= 21:
            invLoop()
    except Exception as exception:
        clearScreen()
        print("'%s' isn't an integer."%strength2)
        Strength2()
def Skill2():
    try:
        global skill2
        skill2 = int(input("%s, please enter your desired skill - between 1 and 20\n>"%name2))
        def invLoop():
            clearScreen()
            Invalid()
            Skill2()
        if int(skill2) <= 0:
                 invLoop()
        if int(skill2) >= 21:
                invLoop()
    except Exception as exception:
        clearScreen()
        print("'%s' isn't an integer."%skill2)
        Skill2()

这是我的错误:

Traceback (most recent call last):
  File "H:\Toby Reichelt\A453\Two Encounters - Developing Dice.py", line 29, in Skill1
    skill1 = int(input("%s, please enter your desired skill - between 1 and 20\n>"%name1))
ValueError: invalid literal for int() with base 10: '0.5'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "H:\Toby Reichelt\A453\Two Encounters - Developing Dice.py", line 197, in <module>
    mainloop()
  File "H:\Toby Reichelt\A453\Two Encounters - Developing Dice.py", line 188, in mainloop
    Skill1()
  File "H:\Toby Reichelt\A453\Two Encounters - Developing Dice.py", line 41, in Skill1
    print("'%s isn't an integer."%skill1)
NameError: global name 'skill1' is not defined

5 个答案:

答案 0 :(得分:1)

skill1未定义,因为如果int(input())失败并引发异常,则根本不会将任何内容分配给skill1

首先将字符串分配给变量,然后尝试转换。

try:
    global skill1
    skill1 = input("%s, please enter your desired skill - between 1 and 20\n>"%name1)
    #if the below line crashes, `skill1` will still have the old string value
    skill1 = int(skill1)

答案 1 :(得分:1)

如果您的int(input('...'))来电失败,则会在将任何内容分配到Exception 之前提出<{1}} skill1的值,但该名称尚未定义。

从异常处理中删除skill1(以及skill1strength1skill2),或者为其分配一些默认值(strength2习惯性的,或有时None}在-1块之外,以确定它们已被定义,即使用户输入了错误的值。


这与全局变量有 nothing ;事实上,在您的代码中根据任何原因声明tryskill1 strength1global并不明确。为了清楚起见,您应该检查docs上的global keyword定义名称;它所做的就是向解析器指出本地范围内对该名称的任何赋值都应该应用于模块级范围内的该名称。

答案 2 :(得分:0)

您似乎期望如果用户的输入不是有效整数,skill1将被定义为......某事。也许是用户输入的原始文本?无论如何,这不会发生什么。

如果int(input(...))引发异常,则控件会转到except块,而不会分配skill1。如果您想要输入即使输入无效也需要打印,您需要单独保存input的结果,而不是将其转换为整数。

答案 3 :(得分:0)

global关键字并不意味着“在全局范围内定义变量”,而是“在搜索要修改的变量时,查看全局范围,而不是本地范围”。

您的代码无论如何都不会运行,您无法调用已定义的函数。

在模块中定义变量skill1skill2strength1strength2,而不是在函数中,它将解决您遇到的问题。

PS。全局变量很难看。不要这样做。

答案 4 :(得分:0)

全局变量无法工作

如果你不尝试,它就不会工作Cuz:除了:“失败不会有任何技能1 为什么不在功能之外定义你的变量?除非你得到同名的其他人。

我不建议使用全局变量