Hi, sorry, noob question. I’m trying to call out and edit products created in my list but keep having the error “‘Product’ object is not subscriptable” when I run below.
The results I want is as per below (index no. + productName):
- Orange
- Apple
- Kiwi
- NewProduct Name
Thereafter to select the index no. to edit the product. Can anyone enlighten me how I can call out the productName so that I can edit from there? Many thanks.
class Product:
def __init__(self, name, country, price):
self.__name = name
self.__country = country
self.__price = price
def getName(self):
return self.__name
def setName(self, name):
self.__name = name
def getCountry(self):
return self.__country
def setCountry(self, country):
self.__description = country
def getPrice(self):
return self.__price
def setPrice(self, price):
self.__price = price
def __str__(self):
string = "[" + self.__name + ", "
string += self.__country + ", "
string += str(self.__price) + "]"
return string
def __repr__(self):
return f"[{self.__name}, {self.__country}, {self.__price}]"
from Fruits import Product
products =
products.append(Product(“Orange”, “Australia”, 0.8))
products.append(Product(“Apple”, “Japan”, 0.7))
products.append(Product(“Kiwi”, “New Zealand”, 1.1))
def PrintProductDetails(product):
print("Name:", product.getName())
print("Country:", product.getCountry())
print("Price: ${:.2f}".format(product.getPrice()))
productName = input("Name: ")
productCountry = input("Country: ")
productPrice = float(input("Price: "))
NewProduct = Product(productName, productCountry, productPrice)
products.append(NewProduct)
print(products)
#Edit products
print(“Product List”)
for index in range(len(products)):
print(str(index+1) +". ", products[index][0])
editProductIndex = int(input(“Select product to edit\n”))
Thanks.