为什么代码无法识别分配的变量?

时间:2019-05-31 15:06:32

标签: python

我正在尝试用Python 3编写一个程序,该程序需要一个大于1970的整数年作为输入,并返回从1970到该年的温度升高。给出了公式和常数。 Python抛出一个错误:

File "4.5.py", line 28, in <module>
    int_years = int_year - 1970
NameError: name 'int_year' is not defined

我是Python的初学者,因此我浏览了可能的解决方案,但找不到任何可行的解决方案。

def user_input():
    while True:
        int_year = int(input("Please enter a year greater than 1970 " ))
        try:
            if int_year > 1970:
                break
            else:
                print("Please enter a year greater than 1970")
        except ValueError:
            print ("It is not a valid year. Try again. ")
    return int_year

"""CO2 level of January 1970"""
c0 = 325.03

"""Current levels of CO2"""
c1 = 411.97

"""Difference in CO2 levels between 1970 and now"""
differenceCO = c1-c0

"""The average CO2 increase per year since 1970"""
per_year_changedCO = ((differenceCO)/(2019-1970))

"""Diffrence in years between 1970 and user input year"""
int_years = int_year - 1970

"""A projected CO2 level in user input year"""
int_year_changedCO = c0+((int_years)*(per_year_changedCO))

"""A projected RF in any year"""
RF = 5.35*(math.log((int_year_changedCO)/(c0)))

"""Increase in temperature from 1970 to user input year"""
def predict_increase():
  temp_int_year = 0.5 * RF
  return temp_int_year

print(temp_int_year)

我希望程序能够识别我在函数中使用的变量。总的来说,我将感谢您对代码的任何评论。

4 个答案:

答案 0 :(得分:4)

在执行行int_years = int_year - 1970时,尚未在范围中定义int_year。到目前为止,您唯一使用过它的地方是在user_input()函数内-但是在函数内定义的变量仅在 内定义内部-不在外部其中。要在函数之外获取int_year的值,您需要调用函数:

int_year = user_input()
int_years = int_year - 1970

或一次完成两个任务:

int_years = user_input() - 1970

答案 1 :(得分:0)

问题在这里:

return int_year

通过插入此指令,您告诉解释器返回当时具有的值int_year 。此变量的任何进一步使用都会导致错误,因为不再定义该变量

答案 2 :(得分:0)

您在user_input的范围内定义了int_year,但是无法使用该功能(more info about that here)

访问

您不是要直接引用变量,而是要获取函数的返回值(由于行“ return int_year”,它是int_year的值):

int_years = user_input() - 1970

答案 3 :(得分:0)

您不会在任何地方调用函数,变量temp_int_year和int_year为Local variables of functions,并且在函数调用完成后无法从外部访问它们

您需要调用函数并将返回值保存到temp_int_year和int_year:

int_year = user_input()

temp_int_year = predict_increase()
print(temp_int_year)
  

注意:您在temp_int_year时遇到了相同的错误

完整代码:

import math
def user_input():
    while True:
        int_year = int(input("Please enter a year greater than 1970 " ))
        try:
            if int_year > 1970:
                break
            else:
                print("Please enter a year greater than 1970")
        except ValueError:
            print ("It is not a valid year. Try again. ")
    return int_year

int_year = user_input()


"""CO2 level of January 1970"""
c0 = 325.03

"""Current levels of CO2"""
c1 = 411.97

"""Difference in CO2 levels between 1970 and now"""
differenceCO = c1-c0

"""The average CO2 increase per year since 1970"""
per_year_changedCO = ((differenceCO)/(2019-1970))

"""Diffrence in years between 1970 and user input year"""
int_years = int_year - 1970

"""A projected CO2 level in user input year"""
int_year_changedCO = c0+((int_years)*(per_year_changedCO))

"""A projected RF in any year"""
RF = 5.35*(math.log((int_year_changedCO)/(c0)))

"""Increase in temperature from 1970 to user input year"""
def predict_increase():
  temp_int_year = 0.5 * RF
  return temp_int_year

temp_int_year = predict_increase()
print(temp_int_year)