I am new to python, and trying to create a program which opens a csv file. The user is supposed to enter a barcode , then the program finds that product and the cost of the product. However I got an error which is the title of my thread. Here is my code.
import csv # imports should go at the top of the file
def read_csv_file():
""" reads csv data and appends each row to list """
csv_data = []
with open("task2.csv") as csvfile:
spamreader = csv.reader(csvfile, delimiter=",", quotechar="|")
for row in spamreader:
csv_data.append(row)
return csv_data
def get_user_input():
""" get input from user """
while True:
try:
GTIN = int(input("input your gtin-8 number: "))
break
except:
print ("Oops! That was not a valid number. Try again")
def search_user_input():
""" search csv data for string """
search_user_input
gtin = get_user_input()
for row in PRODUCT_DATA:
#print(row) #debug print
if row[0] == str(gtin):
product = row[1]
price = round(float(row[2]),2)
print(product, price)
return(product, price)
repeat = input("not in there? search again? If so (y), else press enter to continue")
if repeat == 'y':
search_user_input() # calls function again in order to create a loop
def quantity():
gtin = 0
product = 0
price = 0.0
product_data = read_csv_file()
product,price = search_user_input()
product, price = search_user_input(str(gtin), product_price)
order = int(input("How much of " + product + " do you want?"))
price = round(price * order, 2)
print(quantity,price)
def order_making():
print("Apples")
PRODUCT_DATA = read_csv_file() # call function to read csv
quantity() # call the main function