'NameError: name 'product_name' is not defined, but I defined it?

this is my code:

def get_input():
    
    product_name = input('Enter the name of a product: ')
    price_product = float(input('Enter the current price of the product: ')) #five
    price_one_year_ago = float(input('Enter the price of the product from one year ago: '))
    price_two_years_ago = float(input('Enter the price of the product two years ago: '))
    results(product_name, price_product, price_one_year_ago, price_two_years_ago)


for i in range(0,5):
    get_input()
    results(product_name, price_product, price_one_year_ago, price_two_years_ago)
    print('Product          1 year ago to current          Two years ago to current          Change')
    output_results()
     
def results(product_name, price_product, price_one_year_ago, price_two_years_ago):
    inflation_one_year = ((price_product - price_one_year_ago)/price_one_year_ago* 100)
    inflation_two_years = ((price_product - price_two_years_ago)/price_one_year_ago * 100)

    if inflation_one_year > inflation_two_years:
        change = 'decrease'
    else:
        change = 'increase'

    results = product_name + '          ' + str(inflation_one_year) + '          ' + str(inflation_two_years)+ '          '+ change + '\n'

def output_results():
    print(results)
    aFile = open('inflation.txt', 'a')
    aFile.write(data + '\n')
    aFile.close()

get_input()

try:
    fh = open('inflation.txt')
    for line in fh:
        print(line)
except IOError:
    print("file could not be read")


Solution 1:[1]

You have some syntax errors. So I change the code, it's working, please check the logic of the changed code, maybe it doesn't meet the requirements of the essence of the task.

#declaring functions
def results(product_name, price_product, price_one_year_ago, price_two_years_ago):
        inflation_one_year = ((price_product - price_one_year_ago)/price_one_year_ago* 100)
        inflation_two_years = ((price_product - price_two_years_ago)/price_one_year_ago * 100)
        return inflation_one_year, inflation_two_years

def get_input():
    
    product_name = input('Enter the name of a product: ')
    price_product = float(input('Enter the current price of the product: ')) #five
    price_one_year_ago = float(input('Enter the price of the product from one year ago: '))
    price_two_years_ago = float(input('Enter the price of the product two years ago: '))
    return product_name, price_product, price_one_year_ago, price_two_years_ago

def output_results():
    print(results)
    aFile = open('inflation.txt', 'a')
    aFile.write(data + '\n')
    aFile.close()

#program starts from here
for i in range(0,5):
    product_name, price_product, price_one_year_ago, price_two_years_ago = get_input()
    inflation_one_year, inflation_two_years = results(product_name, price_product, price_one_year_ago, price_two_years_ago)
    if inflation_one_year > inflation_two_years:
        change = 'decrease'
    else:
        change = 'increase'
    data = f'Product {product_name} 1 year ago to current {inflation_one_year}. Two years ago to current {inflation_two_years} change {change}.'
    output_results()

try:
    fh = open('inflation.txt')
    for line in fh:
        print(line)
except IOError:
    print("file could not be read")

Solution 2:[2]

You have defined product_name and other variables in the get_input() method scope.

Declare them globally and then pass them into the get_input() method to assign input values.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 625rk01
Solution 2 The Amateur Coder