Apr-05-2018, 05:30 AM
Here is a working version
#!/usr/bin/env python3 #PublicPizzaClass.py class Pizza: #a constructor that initializes 3 attributes for any-topping selfs. #right now, the attributes are public, so code outside of this class #will be able to get and set the values of these attributes. def __init__(self, name, size, price=None, taxPercent=0.05): self.name = name self.size = size #small = $7, medium = $9, large = $10 self.taxPercent = taxPercent self.price = self.getSizePrice(size) if price is None else price #a method that associates a size with a price @staticmethod def getSizePrice(size): if size == "Small": price = 7.00 elif size == "Medium": price = 9.00 elif size == "Large": price = 10.00 return price #a method that uses two attributes to perform a calculation def getTaxAmount(self): return self.price * self.taxPercent #a method that calls another method to perform a calculation def getTotalPrice(self): return self.price + self.getTaxAmount() #create two pizza objects pizza1 = Pizza("Pepperoni", "Large") pizza2 = Pizza("Sausage", "Medium") print("Name: {:s}".format(pizza1.name)) print("Price: {:.2f}".format(pizza1.price)) print("Tax Amount: {:.2f}".format(pizza1.getTaxAmount())) print("Total Price: {:.2f}".format(pizza1.getTotalPrice()))Note that the implicit first argument in methods is usually named 'self' instead of 'pizza'. In this version, the constructor gives you the opportunity to give a price and tax percent, but if you don't give it, it chooses the default (price from size and tax 0.05).