简体   繁体   English

NameError:未定义名称“值”

[英]NameError: name 'value' is not defined

(background first: I am NEW to programming and currently in my very first "intro to programming class" in my college. This is our second assignment dealing with Functions. So far functions have been a pain in the ass for me because they don't really make any sense. ex: you can use miles_gas(gas) but then don't use "miles_gas" anywhere else, but the program still runs??, anyways) (背景第一:我是编程新手,目前在我大学的第一个“编程入门课程”中。这是我们处理函数的第二个任务。到目前为止,函数对我来说一直很痛苦,因为它们没有“真的没有任何意义。例如:你可以使用miles_gas(gas),但不要在其他任何地方使用“miles_gas”,但程序仍然运行??,无论如何)

Okay, I've looked EVERYWHERE online for this and can't find an answer.好吧,我在网上到处找这个,找不到答案。 Everything is using "Exceptions" and "try" and all that advanced stuff.一切都在使用“异常”和“尝试”以及所有高级内容。 I'm NEW so I have no idea what exceptions are, or try, nor do I care to use them considering my teacher hasn't assigned anything like that yet.我是新来的,所以我不知道什么是例外,或者尝试过,考虑到我的老师还没有分配过这样的东西,我也不想使用它们。

My project is to make a program that gives you the assessment value, and the property tax upon entering your property price.我的项目是制作一个程序,在输入您的房产价格时为您提供评估价值和房产税。 Here is the code I came up with (following the video from my class, as well as in the book)这是我想出的代码(按照我班上的视频以及书中的内容)

ASSESSMENT_VALUE = .60
TAX = 0.64

def main():
    price = float(input('Enter the property value: '))
    show_value(value)
    show_tax(tax)

def show_value():
    value = price * ASSESSMENT_VALUE
    print('Your properties assessment value is $', \
        format(value, ',.2f'), \
        sep='')

def show_tax(value,TAX):
    tax = value * TAX
    print('Your property tax will be $', \
        format(tax, ',.2f'), \
        sep='')    

main()

Upon running it, I get it to ask "blah blah enter price:" so I enter price then I get a huge red error saying运行它后,我得到它询问“等等等等输入价格:”所以我输入价格然后我得到一个巨大的红色错误说

Traceback (most recent call last):
      File "C:/Users/Gret/Desktop/chapter3/exercise6.py", line 41, in <module>
      main()
File "C:/Users/Gret/Desktop/chapter3/exercise6.py", line 24, in main
show_value(value)
NameError: name 'value' is not defined

But I DID define 'value'... so why is it giving me an error??但我确实定义了“价值”......那为什么它给我一个错误?

Python is lexically scoped. Python 是词法范围的。 A variable defined in a function isn't visible outside the function.函数中定义的变量在函数外部不可见。 You need to return values from functions and assign them to variables in the scopes where you want to use the values.您需要从函数返回值并将它们分配给要使用这些值的范围内的变量。 In your case, value is local to show_value .在您的情况下, valueshow_value本地value

When you define a function, it needs parameters to take in. You pass those parameters in the brackets of the function, and when you define your function, you name those parameters for the function.当你定义一个函数时,它需要接受参数。你在函数的括号中传递这些参数,当你定义你的函数时,你为函数命名这些参数。 I'll show you an example momentarily.我马上给你看一个例子。

Basically what's happened is you've passed the function a parameter when you call it, but in your definition you don't have one there, so it doesn't know what to do with it.基本上发生的事情是你在调用函数时传递了一个参数,但在你的定义中你没有参数,所以它不知道如何处理它。

Change this line:改变这一行:

def show_value():

To this line:到这一行:

def show_value(price):

And show_value to show_value(price)show_valueshow_value(price)

For example:例如:

In this type of error:在这种类型的错误中:

def addition(a,b):
    c = a + b
    return c

addition() # you're calling the function, 
           # but not telling it the values of a and b

With your error:你的错误:

def addition():
    c = a + b
    return c

addition(1,2) # you're giving it values, but it 
              # has no idea to give those to a and b

The thing about functions, is that those variable only exist in the function, and also the name of the parameters doesn't matter, only the order.关于函数的事情,就是那些变量只存在于函数中,而且参数的名字并不重要,只有顺序。 I understand that's frustrating, but if you carry on programming with a more open mind about it, I guarantee you'll appreciate it.我知道这令人沮丧,但如果您以更开放的心态继续编程,我保证您会感激不尽。 If you want to keep those values, you just need to return them at the end.如果你想保留这些值,你只需要在最后返回它们。 You can return multiple variables by writing return c, a, b and writing the call like this sum, number1, number2 = addition(1,2)您可以通过编写return c, a, b并编写这样的调用来返回多个变量sum, number1, number2 = addition(1,2)

Another problem is that I could call my addition function like this:另一个问题是我可以像这样调用我的加法函数:

b = 1
a = 2
addition(b,a)

and now inside the function, a = 1 and b = 2, because it's not about the variable names, it's about the order I passed them to the function in.现在在函数内部,a = 1 和 b = 2,因为它不是关于变量名,而是关于我将它们传递给函数的顺序。

You also don't need to pass TAX into show_tax because TAX is already a global variable.您也不需要将 TAX 传递给 show_tax,因为 TAX 已经是一个全局变量。 It was defined outside a function so it can be used anywhere.它是在函数之外定义的,因此可以在任何地方使用。 Additionally, you don't want to pass tax to show_tax, you want to pass value to it.此外,您不想将税金传递给 show_tax,而是希望将值传递给它。 But because show_value hasn't returned value, you've lost it.但是因为 show_value 没有返回值,你已经失去了它。 So return value in show value to a variable like so value = show_value(price) .因此,将 show value 中的值返回给变量,例如value = show_value(price)

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

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