I am trying to create an online shopping cart system in python to understand list better but have come across some struggles.
I am having difficulties with displaying information using a list. I have already created a list where the person writes in the product the code into an empty list as shown below.
else:
code_input.append(i)
quan_input.append(s)
code_inputed.append(code_input)
quan_inputed.append(quan_input)
I want to use the list with the product codes to find the correlating name and price by using trying to use the code_input list as an index to find the items in the other list.
I have written the simple code to try to find if it works but it comes up with TypeError: list indices must be integers or slices, not list
The code
def display():
index = code_inputed
for i in code_inputed: # and index in range(len(productList)):
print(index)
print(productList[index], quan_inputed[index])
Any help would be greatly appreciated and I'm sorry if none of this makes any sense I am only new.
Thank you
CodePudding user response:
It is a bit unclear what you want, but do you perhaps mean:
def display():
index = code_inputed
for i in code_inputed: # and index in range(len(productList)):
print(i) #CHANGED TO i!
print(productList[i], quan_inputed[i])
CodePudding user response:
def display():
index = code_inputed
for i in code_inputed: # and index in range(len(productList)):
print(i)
print(productList[i], quan_inputed[i])
where "i" represent an item in the code_inputed
list and item in quan_inputed
list
I think what you want to achieve is to tie product code for the actual product.
Maybe consider using dict
?
myShop= [
{
"productName": "Table",
"color": "Red",
"price": "79",
"productCode": "7546374"
},
{
"productName": "Smart Tv",
"color": "White",
"price": "159",
"productCode": "7546375"
}
]
# Print products
def print_products():
for product in myShop:
print(
"[ " product["productName"] " ]",
"[ " product["color"] " ]",
"[ " product["price"] " ]",
"[ " product["productCode"] " ]",
sep = " - ")
print_products()