为什么代码不能识别指定的变量?

2024-10-08 18:27:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python3编写一个程序,输入大于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)

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


Tags: in程序inputisyeartempintco2
3条回答

你不能在任何地方调用你的函数,变量temp\u int\u year和int\u year是Local variables of functions,当函数调用完成后,它们不能从外部访问

您需要调用函数并将返回值保存到temp\u int\u year和int\u year中:

int_year = user_input()

temp_int_year = predict_increase()
print(temp_int_year)

NOTE: you have the same mistake with 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)

当您执行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

问题在于:

return int_year

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

相关问题 更多 >

    热门问题