简体   繁体   English

在python脚本中处理变量范围

[英]Handling variable scope in a python script

I am learning to code in Python. 我正在学习用Python编写代码。 I am creating a program that will perform unit conversions, however I have been stuck on this error for a good while: 我正在创建一个将执行单位转换的程序,但是我在这个错误上停留了很长时间:

NameError: name 'ini' is not defined NameError:未定义名称“ ini”

Here is the code: 这是代码:

a = ("Distance")
b = ("Time")
c = ("Volume")
d = ("Temp")
e = ("Weight")

def conversion_type(first):
    if first == ("Distance"):
        ini = input("How great a distance am I converting?\n")
    elif first == ("Time"):
        ini = input("How much time am I converting?\n")
    elif first == ("Volume"):
        ini = input("How great a volume am I converting?\n")
    elif first == ("Temp"):
        ini = input("How many degrees am I converting?\n")
    elif first == ("Weight"):
        ini = input("How much weight am I converting?\n")
    else:
        print("That not was an afformentioned dimension you dolt.")

def variable_type_converter(ini):
    ini = float(ini)

print ("\n    Welcome to the Convert-O-Matic\n==============================\n")
print ("I support the following dimensions:\n")
print ("%s, %s, %s, %s, and %s," % (a,b,c,d,e))
first = input("What kind of conversion would you like to do?\n")
conversion_type(first)
variable_type_converter(ini)
print("==========================================")

ini is not declared in the global scope, only inside of functions (it is used in conversion_type() and therefore implicitly declared there, and it is an argument to variable_type_converter() ). ini不在全局范围内声明,仅在函数内部声明(在conversion_type() ,因此在其中隐式声明,并且它是variable_type_converter()的参数)。 But since it was not declared in the global scope, it does not exist there. 但是,由于未在全局范围内声明它,因此它不存在。 If you want to set the value of ini in conversion_type() and have the value be usable elsewhere, declare a value for ini somewhere before you call conversion_type() . 如果要在conversion_type()设置ini的值并使该值可在其他地方使用,请在调用conversion_type()之前在某处声明ini的值。

This answer has a good summary of Python's scoping rules. 这个答案很好地总结了Python的作用域规则。

Update: As MadWombat points out in comments, your variable_type_converter() function doesn't do anything. 更新:正如MadWombat在注释中指出的那样,您的variable_type_converter()函数不执行任何操作。 It takes one argument, called ini , it casts it as float and reassigns back to ini , but then it doesn't return a value. 它接受一个称为ini参数,将其强制转换为float并重新分配回ini ,但随后不返回值。 So when the variable_type_converter() function exits, the value is discarded and and the float cast is never used. 因此,当variable_type_converter()函数退出时,该值将被丢弃,并且从不使用float强制转换。

When defining your variables you should use a = "Distance" . 定义变量时,应使用a = "Distance" You should also check the conversion type using first == "Distance" 您还应该使用first == "Distance"检查转换类型

ini is not defined, is occurring because the function conversion_type(first) is returning a value that is not stored. 由于函数conversion_type(first)返回的值未存储,因此未定义ini Try: 尝试:

# get conversion type 
conversion_type_chosen = conversion_type(first)

# calculate value
converted_value = variable_type_convertor(conversion_type_chosen)

# print result
print "result: {}".format(converted_value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM