零售商品分类作业
这个程序应该是用来获取3个物品的信息,存储这些信息,然后以如下方式显示出来:
物品 1
物品 2
物品 3
但是我遇到了这个错误:第47行,在主函数中,list_display(inventory)出现了NameError:全局名称'list_display'未定义。
我刚开始学这个,肯定是哪里疏忽了,但我就是搞不明白。任何帮助都非常感谢。
# This RetailItem class holds data about products.
class RetailItem:
# __int__ method initializes the attributes.
def __init__(self, description, units, price):
self.__item_description = description
self.__units_in_inventory = units
self.__price = price
# The set_item_description method gets the item type.
def set_item_description(self, description):
self.__item_description = description
# The set_units_in_inventory method gets number of items available.
def set_units_in_inventory(self, units):
self.__units_in_inventory = units
# The set_price method gets the cost of item.
def set_price(self, price):
self.__price = price
# The get_item_description method returns the item type.
def get_item_description(self):
return self.__item_description
# The get_units_in_inventory returns the number of items available.
def get_units_in_inventory(self):
return self.__units_in_inventory
# The get_price method returns the cost of item.
def get_price(self):
return self.__price
# This program will test the RetailItem class and return information
# using the mutator method.
import sys
# This defines the main function.
def main():
# Get a list of RetailItem objects.
inventory = make_list()
# Display the data in the list.
print('Here is the data you entered:')
list_display(inventory)
# The make_list will get data for three items. It will
#return a list of available items.
def make_list():
# Create an empty list.
item_list = []
# Add three item to the list.
print('Enter data for three items.')
for count in range(1, 4):
# Get item data.
print('Item number ' + str(count) + ':')
item = input('Enter description of item: ')
units = float(input('Enter number of units in inventory: '))
price = float(input('Enter price per item: '))
print()
# Creat new RetailItem and assign items variable.
items = RetailItem(item, units, price)
# Add items to list.
item_list.append(items)
return item_list
#Display the items information.
def display_list(item_list):
for item in item_list:
print(item.get_item_description())
print(item.get_units_in_inventory())
print(item.get_price())
print()
# Call the main function.
main()
1 个回答
0
你的问题是你调用了错误的函数,list_display(inventory)
,而你在make_list()
里面定义了display_list(item_list)
。
我把display_list(item_list)
从make_list()
中移出来,并把list_display(inventory)
改成了display_list(inventory)
。
# This RetailItem class holds data about products.
class RetailItem:
# __int__ method initializes the attributes.
def __init__(self, description, units, price):
self.__item_description = description
self.__units_in_inventory = units
self.__price = price
# The set_item_description method gets the item type.
def set_item_description(self, description):
self.__item_description = description
# The set_units_in_inventory method gets number of items available.
def set_units_in_inventory(self, units):
self.__units_in_inventory = units
# The set_price method gets the cost of item.
def set_price(self, price):
self.__price = price
# The get_item_description method returns the item type.
def get_item_description(self):
return self.__item_description
# The get_units_in_inventory returns the number of items available.
def get_units_in_inventory(self):
return self.__units_in_inventory
# The get_price method returns the cost of item.
def get_price(self):
return self.__price
# This program will test the RetailItem class and return information
# using the mutator method.
import sys
# This defines the main function.
def main():
# Get a list of RetailItem objects.
inventory = make_list()
# Display the data in the list.
print('Here is the data you entered:')
display_list(inventory)
# The make_list will get data for three items. It will
#return a list of available items.
def make_list():
# Create an empty list.
item_list = []
# Add three item to the list.
print('Enter data for three items.')
for count in range(1, 4):
# Get item data.
print('Item number ' + str(count) + ':')
item = input('Enter description of item: ')
units = float(input('Enter number of units in inventory: '))
price = float(input('Enter price per item: '))
print()
# Creat new RetailItem and assign items variable.
items = RetailItem(item, units, price)
# Add items to list.
item_list.append(items)
return item_list
#Display the items information.
def display_list(item_list):
for item in item_list:
print(item.get_item_description())
print(item.get_units_in_inventory())
print(item.get_price())
print()
# Call the main function.
main()