python - Why do I get an attribute error with my class? -
edit below!
here retail_item class:
#retailitem class class retailitem: def __init__(self, desc, inventory, price): self.__desc=desc self.__inventory=inventory self.__price=price #mutators def set_desc (self, desc): self.__desc=desc def set_inventory (self, inventory): self.__inventory=inventory def set_price (self, price): self.__price = price #accessors def get_desc(self): return self.__desc def get_inventory(self): return self.__inventory def get_price(self): return self.__price def __str__(self): return 'item description:' + self.__desc, \ '\tnumber of units:' + self.__inventory, \ '\tprice: $' + self.__price
and cash_register class:
#cashregister class class cashregister: def __init__(self, purchase, total, show, clear): self.__purchase=purchase self.__total=total self.__show=show self.__clear=clear #mutators def purchase_item(self, purchase): self.__purchase=purchase def get_total(self, total): self.__total=total def show_item(self, show): self.__show=show def clear(self, clear): self.__clear=clear #accessors def acc_purchase(self): return self.__purchase def acc_total(self): return self.__total def acc_show(self): return self.__show def acc_clear(self): return self.__clear
and program:
import retail_item import cash_register show = 1 purchase = 2 cart = 3 total = 4 empty = 5 quit = 6 def main(): mylist = make_list() #mycr = cash_register.cashregister(mylist) choice = 0 # process menu selections until user quits program. while choice != quit: # user's menu choice. choice = get_menu_choice() # proces choice. if choice == show: show_items(mylist) elif choice == purchase: purchase_item(mylist) elif choice == total: get_total(mylist) elif choice == empty: clear(mylist) def make_list(): item_list = {} desc = 'jacket' inventory = 12 price = 59.95 entry = retail_item.retailitem(desc, inventory, price) item_list[desc]=entry desc = 'jeans' inventory = 40 price = 34.95 entry = retail_item.retailitem(desc, inventory, price) item_list[desc]=entry desc = 'shirt' inventory = 20 price = 24.95 entry = retail_item.retailitem(desc, inventory, price) item_list[desc]=entry return item_list # get_menu_choice function displays menu , gets # validated choice user. def get_menu_choice(): print() print('cash register menu') print('-------------------------') print('1. show retial items') print('2. purchase item(s)') print('3. show current shopping cart') print('4. show total of items purchased') print('5. empty shopping cart') print('6. quit program') print() # user's choice. choice = int(input('enter choice: ')) # validate choice. while choice < show or choice > quit: choice = int(input('enter valid choice: ')) # return user's choice. return choice def show_items(mylist): print('\t\tdescription\t\tunits in inventory\t\tprice') print('--------------------------------------------------------------------------------') x=1 item in mylist: print('item #', x, '\t\t', item.get_desc(), '\t\t\t\t', item.get_inventory(), '\t\t\t$', format(item.get_price(), ',.2f'),sep='') print() x+=1 def purchase_item(mylist): desc = input('enter item wish purchase: ') if desc in mylist: amount=int(input('how many buy: ')) if mylist[units]>0: mylist[units]-=amount elif (units-amount<0): mylist[units]=0 else: mylist[units] = 0 entry=cash_register.cashregister(desc, units,) mylist[desc]=entry print() def get_total(mylist): print() def clear(mylist): print(mylist) mylist.clear() print(mylist) main()
so question is, how update 1 object of class? , how call on cash_register class?
here instructions assignment, if helps:
this exercise assumes have created retailitem class programming exercise 5. create cashregister class can used retailitem class. cashregister class should able internally keep list of retailitem objects. class should have following methods: • method named purchase_item accepts retailitem object argument. each time purchase_item method called, retailitem object passed argument should added list. • method named get_total returns total price of retailitem objects stored in cashregister object’s internal list. • method named show_items displays data retailitem objects stored in cashregister object’s internal list. • method named clear should clear cashregister object’s internal list. demonstrate cashregister class in program allows user select several items purchase. when user ready check out, program should display list of items or has selected purchase, total price.
edit: here's final code. know it's not pretty, , apologize lack of comments. still feedback though i'll submitting shortly (for own betterment , job opportunities!) here is:
import retail_item import cash_register show = 1 purchase = 2 total = 3 empty = 4 quit = 5 def main(): #set variables 0 lister = [] inv=[] cost=[] desc='' inventory=0 price=0 total=0 purchase=0 #setting variable each class cash=cash_register.cashregister(purchase, total, lister, inv, cost) retail=retail_item.retailitem(desc, inventory, price) #classes desc = 'jacket' inventory = 12 price = 59.95 #setting classes retail.set_desc(desc) retail.set_inventory(inventory) retail.set_price(price) #adding cart cash.purchase_item(retail.get_desc(), lister) cash.purchase_item(retail.get_inventory(), inv) cash.purchase_item(retail.get_price(), cost) desc = 'jeans' inventory = 40 price = 34.95 retail.set_desc(desc) retail.set_inventory(inventory) retail.set_price(price) cash.purchase_item(retail.get_desc(), lister) cash.purchase_item(retail.get_inventory(), inv) cash.purchase_item(retail.get_price(), cost) desc = 'shirt' inventory = 20 price = 24.95 retail.set_desc(desc) retail.set_inventory(inventory) retail.set_price(price) cash.purchase_item(retail.get_desc(), lister) cash.purchase_item(retail.get_inventory(), inv) cash.purchase_item(retail.get_price(), cost) choice = 0 # process menu selections until user quits program. while choice != quit: # user's menu choice. choice = get_menu_choice() # proces choice. if choice == show: show_items(cash, retail, lister, inv, cost) elif choice == purchase: purchase_item(cash, retail, lister, inv, cost) elif choice == total: get_total(cash, retail, lister) elif choice == empty: price=0 cash.set_total(price) clear(cash, lister) # get_menu_choice function displays menu , gets # validated choice user. def get_menu_choice(): print() print('cash register menu') print('-------------------------') print('1. show retail items') print('2. purchase item(s)') print('3. show total of items purchased') print('4. empty shopping cart') print('5. quit program') print() # user's choice. choice = int(input('enter choice: ')) # validate choice. while choice < show or choice > quit: choice = int(input('please enter valid choice: ')) # return user's choice. return choice def show_items(cash, retail, lister, inv, cost): print('\t\tdescription\t\tunits in inventory\t\tprice') print('--------------------------------------------------------------------------------') cash.show_item(lister, inv, cost) def purchase_item(cash, retail, lister, inv, cost): jacket=1 jeans=2 shirt=3 quit=4 choice=0 print() print('which buy') print('-------------------------') print('1. jacket') print('2. jeans') print('3. shirt') print('4. quit') print() print('choose many like. press 4 enter quit.') while choice != quit: # user's menu choice. choice = int(input('which buy: ')) if choice < jacket or choice > quit: choice = int(input('please enter valid choice: ')) while choice != quit: # proces choice. if choice == jacket: desc = 'jacket' inventory = 12 price = 59.95 retail.set_desc(desc) retail.set_inventory(inventory) retail.set_price(price) cash.purchase_item(retail.get_desc(), lister) cash.purchase_item(retail.get_inventory(), inv) cash.purchase_item(retail.get_price(), cost) cash.set_total(price) break elif choice == jeans: desc = 'jeans' inventory = 40 price = 34.95 retail.set_desc(desc) retail.set_inventory(inventory) retail.set_price(price) cash.purchase_item(retail.get_desc(), lister) cash.purchase_item(retail.get_inventory(), inv) cash.purchase_item(retail.get_price(), cost) cash.set_total(price) break elif choice == shirt: desc = 'shirt' inventory = 20 price = 24.95 retail.set_desc(desc) retail.set_inventory(inventory) retail.set_price(price) cash.purchase_item(retail.get_desc(), lister) cash.purchase_item(retail.get_inventory(), inv) cash.purchase_item(retail.get_price(), cost) cash.set_total(price) break print() def get_total(cash, retail, lister): print() cash.show_items(cash.get_list(lister)) print('your total is: $', format(cash.cost_total(),',.2f')) def clear(cash, lister): print('shopping cart emptied.') lister=lister.clear() price=0 cash.set_total(price) return lister main()
retailitem class:
class retailitem: def __init__(self, desc, inventory, price): self.__desc=desc self.__inventory=inventory self.__price=price #mutators def set_desc (self, desc): self.__desc=desc def set_inventory (self, inventory): self.__inventory=inventory def set_price (self, price): self.__price = price #accessors def get_desc(self): return self.__desc def get_inventory(self): return self.__inventory def get_price(self): return self.__price def __str__(self): return 'item description:' + self.__desc, \ '\tnumber of units:' + self.__inventory, \ '\tprice: $' + self.__price
and again, lastly cashregister class:
#cashregister class class cashregister: def __init__(self, purchase, total, lister, inv, cost): self.__purchase=purchase self.__total=total self.__lister=[] self.__inv=[] self.__cost=[] #mutators def purchase_item(self, purchase, lister): self.__purchase=purchase lister.append(purchase) return lister def set_total(self, price): self.__total+=price def show_item(self, lister, inventory, price): i=0 while i<len(lister): s=('item # %i\t%s\t\t\t\t%i\t\t\t%4.2f') % ((i+1),lister[i],inventory[i],price[i]) s = s.strip(' \t\n\r') print(s) i+=1 def show_items(self, lister): i=0 print('you have purchased following items') while i<len(lister): print(lister[i]) i+=1 def clear(self, lister): i=0 while i<len(lister): del lister[i] i+=1 return lister def get_list(self, lister): return lister #accessors def acc_purchase(self): return self.__purchase def cost_total(self): return self.__total def acc_show(self): return self.__show def acc_clear(self): return self.__clear
thanks again guys! used site often, , though didn't use of y'all gave me time, you're still awesome!
this code has couple of major problems. without stack trace can't why you're getting attributeerror, can tell cashregister objects can't instantiated written. __init__
refers nonexistent variables - item , items.
your cashregister class taking arguments __init__
aren't necessary - , in cases should methods. don't see reason cashregister class should take __init__
arguments - should initialize list of retailitem objects , running total , nothing else. purchase_item should update internal attributes, get_total , show_items should read them (or get_total should calculate total based on list if don't keep running total) , clear should reset them both.
stylistically, mutators , accessors , hidden internal data not pythonic idiom. in general python classes , set member data directly, , refactored use properties if behavior ever needs change. see class/exercise, might using them because they're required, if not you're better off without them.
edit
this main() like, making minimal edits show logic:
def main(): items = make_list() mycr = cash_register.cashregister() # @ point, mycr.show_items() should return [] , mycr.get_total() should return 0 choice = 0 while choice != quit: choice = get_menu_choice() if choice == show: mycr.show_items() elif choice == purchase: item, quantity = get_purchase(items) if item , quantity: item.set_inventory(max(0, item.get_inventory() - quantity)) mycr.purchase_item(item, quantity) elif choice == total: print(mycr.get_total()) elif choice == empty: mycr.clear() def get_purchase(items): desc = input('enter item wish purchase: ') if desc in items: amount=int(input('how many buy: ')) return items[desc], amount else: return none, 0
this doesn't cover every possibility - instance, original code allows entering higher quantities available no side effect besides setting quantity 0. exercise description doesn't mention inventory tracking, maybe that's not requirement.
Comments
Post a Comment