Margarita Shablya
Margarita Shablya

Reputation: 3

Why the code doesn't recognize the assigned variable?

I am trying to write a program in Python 3 that takes an int year larger than 1970 as an input and returns the increase in temperature from 1970 until that year. The formulas and constants are given. Python throws me an error:

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

I am a beginner in Python so I have browsed possible solutions, but could not find any working ones.

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)

I expect the program to recognize the variables I use in functions. Overall, I will appreciate any comments on the code.

Upvotes: 0

Views: 78

Answers (4)

Brae
Brae

Reputation: 514

You define int_year within the scope of the user_input, but that is not accessible outwith that function (more info about that here)

Rather than reference the variable directly you want to get the return value of the function (which is the value of int_year because of the line 'return int_year'):

int_years = user_input() - 1970

Edit for comment above

def user_input():
    try:
        int_year = int(input("Please enter a year greater than 1970"))
        if int_year < 1970:
            print("Invalid year, please enter a value greater than 1970")
            int_year = user_input()
    except ValueError:
        print("Invalid input. Please enter a number")
        int_year = user_input()
    return int_year

Upvotes: 0

ncica
ncica

Reputation: 7206

you dont call your functions anywhere, variables temp_int_year and int_year are Local variables of functions and they can't be accessed from outside, when the function call has finished

you need to call function and save return value into temp_int_year and int_year:

int_year = user_input()

temp_int_year = predict_increase()
print(temp_int_year)

NOTE: you have the same mistake with temp_int_year

full code:

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)

Upvotes: 0

Bogdan Doicin
Bogdan Doicin

Reputation: 2416

The problem is here:

return int_year

By inserting this instruction, you tell the interpreter to return the value int_year has at that moment. Any further usage of this variable leads to an error, because the variable is no longer defined

Upvotes: 0

Green Cloak Guy
Green Cloak Guy

Reputation: 24711

By the time you execute the line int_years = int_year - 1970, you haven't defined int_year in scope. The only place you've used it up to that point is within the user_input() function - but variables defined within functions are only defined within the functions - not outside of them. To get the value of int_year outside of the function, you need to call the function:

int_year = user_input()
int_years = int_year - 1970

or simply do them both at once:

int_years = user_input() - 1970

Upvotes: 4

Related Questions