I am trying to call a function in Python but I get the error:
File "<exec>", line 11, in <module> NameError: name 'item_bought_1' is not defined`
I am making a simple program in Python that asks the user for an item they have bought, the price and quantity of items bought. It later on displays the input in a table format. I have not included that code here. Here is the relevant initial code (that asks the user to key in the item, price and quantity, as well as computes the total per item):
#ask the user what item they intend to buy as well as the unit price and number of items they intend to buy
item_bought_1 = input("What item has been bought?")
unit_price_1 = input("How much is each unit of " + item_bought_1)
quantity_1 = input("How many units of "+ item_bought_1 + " have been bought?")
item_total_1 = int(unit_price_1) * int(quantity_1)
item_bought_2 = input("ITEM 2::: What item has been bought?")
unit_price_2 = input("How much is each unit of " + item_bought_2)
quantity_2 = input("How many units of "+ item_bought_2 + " have been bought?")
item_total_2 = int(unit_price_2) * int(quantity_2)
item_bought_3 = input("ITEM 3:::What item has been bought?")
unit_price_3 = input("How much is each unit of " + item_bought_3)
quantity_3 = input("How many units of "+ item_bought_3 + " have been bought?")
item_total_3 = int(unit_price_3) * int(quantity_3)
item_bought_4 = input("iTEM 4:::What item has been bought?")
unit_price_4 = input("How much is each unit of " + item_bought_4)
quantity_4 = input("How many units of "+ item_bought_4 + " have been bought?")
item_total_4 = int(unit_price_4) * int(quantity_4)
I am trying to make the code less repetitive by using a function:
#ask the user what item they intend to buy as well as the unit price and number of items they intend to buy
def question_asker(item_bought, unit_price, quantity, item_total):
item_bought = input("What item has been bought?")
unit_price = input("How much is each unit of " + item_bought)
quantity = input("How many units of "+ item_bought + " have been bought?")
item_total = int(unit_price) * int(quantity)
question_asker(item_bought_1, unit_price_1, quantity_1, item_total_1)
question_asker(item_bought_2, unit_price_2, quantity_2, item_total_2)
question_asker(item_bought_3, unit_price_3, quantity_3, item_total_3)
question_asker(item_bought_4, unit_price_4, quantity_4, item_total_4)
But I get the error:
File "<exec>", line 11, in <module>
NameError: name 'item_bought_1' is not defined
Am I calling the function correctly? Where is my problem?
Remove arguments and return the results:
def question_asker():
item_bought = input("What item has been bought?")
unit_price = input("How much is each unit of " + item_bought)
quantity = input("How many units of "+ item_bought + " have been bought?")
item_total = int(unit_price) * int(quantity)
return item_bought,unit_price,quantity,item_total
item_bought_1, unit_price_1, quantity_1, item_total_1 = question_asker()
item_bought_2, unit_price_2, quantity_2, item_total_2 = question_asker()
item_bought_3, unit_price_3, quantity_3, item_total_3 = question_asker()
item_bought_4, unit_price_4, quantity_4, item_total_4 = question_asker()