Apr-05-2018, 04:05 AM
I'm new to Python Classes. I'm trying to tell the constructor to associate a size with a price, but it's not liking it.
Here is the module code (PublicPizzaClass.py) with the class constructor in it:
Here is the module code (PublicPizzaClass.py) with the class constructor in it:
#!/usr/bin/env python3
#PublicPizzaClass.py
class Pizza:
#a constructor that initializes 3 attributes for any-topping pizzas.
#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__(pizza, name, size, price, taxPercent):
pizza.name = name
pizza.size = size #small = $7, medium = $9, large = $10
pizza.price = getSizePrice()
#a method that associates a size with a price
def getSizePrice(pizza):
if pizza.size == "Small":
pizza.price = 7.00
elif pizza.size == "Medium":
pizza.price = 9.00
elif pizza.size == "Large":
pizza.price = 10.00
return pizza.price
#a method that uses two attributes to perform a calculation
def getTaxAmount(pizza):
pizza.price = getSizePrice()
return pizza.price * 0.05
#a method that calls another method to perform a calculation
def getTotalPrice(pizza):
return pizza.price + getTaxAmount()And here is the code (PublicPizzaClassUser.py) that I'm trying to get to use the module's code:#!/usr/bin/env python3
#PublicPizzaClassUser.py
#import the class from the PublicPizzaClass module
from PublicPizzaClass import Pizza
#create two pizza objects
pizza1 = Pizza("Pepperoni", "Large", getSizePrice())
pizza2 = Pizza("Sausage", "Medium", getSizePrice())
print("Name: {:s}".format(pizza1.name))
print("Price: {:.2f}".format(pizza1.getSizePrice()))
print("Tax Amount: {:.2f}".format(pizza1.getTaxAmount()))
print("Total Price: {:.2f}".format(pizza1.getTotalPrice()))The error is:Error:==== RESTART: I:/Python/Python36-32/SamsPrograms/PublicPizzaClassUser.py ====
Traceback (most recent call last):
File "I:/Python/Python36-32/SamsPrograms/PublicPizzaClassUser.py", line 8, in <module>
pizza1 = Pizza("Pepperoni", "Large", getSizePrice())
NameError: name 'getSizePrice' is not definedIt's starting to look like using function calls to define constructor attributes is a no-no. Is that correct?
