class RetailItem:
# The _init_ method initalizes the attributes.
def __init__(self, description, units, price):
self.__description = description
self.__units = units
self.__price = price
# The set_description method acccetps an argument for the retailitem's description.
def set_description(self, description):
self.__description = description
# The set_units method accepts an argument for the retailitem's units.
def set_units(self, units):
self.__units = units
# the set_price method accepts an argument for the retailitem's price.
def set_price(self, price):
self.__price = price
# The set_description methods returns the retailitem's description.
def get_description(self):
return self.__description
# The get_units method returns the retailitem's units.
def get_units(self):
return self.__units
# The get_price method returns the retailitem's price.
def get_price(self):
return self.__price
def make_list ():
#Create an empty list
item_list = []
# Add Item Object to the list
print ('Enter date for the items')
keep_going = 'Y'
i = 1
while keep_going.upper() == 'Y':
print('Item n*',i)
#Get the Item data
descript = input('Enter the description: ')
units_Inv = input('Enter the units: ')
prix = float(input('Enter the price:$'))
print()
# Create an instance of the retailitem class
item = retailitem.RetailItem(descript, units_Inv, prix)
i += 1
#Add the Object to the list
item_list.append(item)
keep_going = input('Press Y to continue to add Data for item or N to stop: ')
# return the list
return item_list
def display_list(item_list):
for var in item_list:
print(var.get_description())
print(var.get_units())
print(var.get_price())
print()
#call the main function.
main ()

- 79,954
- 26
- 128
- 166

- 1
- 1
6 Answers
main
needs to be defined (def main
doesn't appear in your example, at least). My guess is that you are expecting the following:
def main():
lst = make_list()
display_list(lst)

- 79,954
- 26
- 128
- 166
The problem is exactly what the error message is telling you:
#call the main function.
main ()
Where is main()
defined?
This may be caused by confusion about how Python works vs. other languages. In Python, anything at the global level is executed automatically -- it doesn't need to go in a main function.
So, if you want to run some code, just do it:
class RetailItem:
def __init__(self, description, units, price):
self.__description = description
self.__units = units
self.__price = price
# etc.
l = make_list()
display_list(l)
Typically you would wrap that code at the end in a if __name__ == "__main__"
block, but it's not required.

- 1
- 1

- 53,280
- 21
- 146
- 188
you need somewhere
def main():
#function body - whatever you want it to do
in this case, what it looks like you want to do is to create an instance of the RetailItem class and then call its methods.

- 2,340
- 3
- 17
- 23
The error says the issue. "main is not defined". You are calling the main() function at the end but didn't define it anywhere in the code.
def main():
# this is your main function
list = make_list()
display_list(list)

- 671
- 1
- 8
- 17
w.r.t. "I just want it to run": add the following to the top of your code:
def main():
pass
Your code will now run. Technically.

- 52,157
- 12
- 73
- 112
If you want to reference putting all of the functions together you actually need to declare a function called main() EX:
def main():
makelist()
displaylist(item_list)
####################################
main()

- 696
- 3
- 10
- 31