#Below is my Pizza class ,how would I write the function described in the attached image,based on this class? # The Pizza class should have two attributes(data items): class Pizza: # The Pizza class should have the following methods/operators): # __init__ - # constructs a Pizza of a given size (defaults to ‘M’) # and with a given set of toppings (defaults to empty set). def __init__(self, size='M', toppings=set()): self.size = size self.toppings = toppings # setSize – set pizza size to one of ‘S’,’M’or ‘L’ def setSize(self, size): self.size = size # getSize – returns size
#Python IDLE:
#Below is my Pizza class ,how would I write the function described in the attached image,based on this class?
# The Pizza class should have two attributes(data items):
class Pizza:
# The Pizza class should have the following methods/operators):
# __init__ -
# constructs a Pizza of a given size (defaults to ‘M’)
# and with a given set of toppings (defaults to empty set).
def __init__(self, size='M', toppings=set()):
self.size = size
self.toppings = toppings
# setSize – set pizza size to one of ‘S’,’M’or ‘L’
def setSize(self, size):
self.size = size
# getSize – returns size
def getSize(self):
return self.size
# addTopping – adds a topping to the pizza, no duplicates, i.e., adding ‘pepperoni’ twice only adds it once
def addTopping(self, topping):
self.toppings.add(topping)
# removeTopping – removes a topping from the pizza
def removeTopping(self, topping):
self.toppings.remove(topping)
# price – returns the price of the pizza according to the following scheme:
def price(self):
# ‘S’: $6.25 plus 70 cents per topping
if self.size == 'S':
return 6.25+0.7*len(self.toppings)
# ‘M’: $9.95 plus $1.45 per topping
elif self.size == 'M':
return 9.95+1.45*len(self.toppings)
# ‘L’: $12.95 plus $1.85 per topping
elif self.size == 'L':
return 12.95+1.85*len(self.toppings)
# __repr__ - returns representation as a string –
def __repr__(self):
# Pizza('L',{'anchovies', 'mushrooms', 'pepperoni'})
return f"Pizza('{self.size}',{self.toppings})"
# __eq__ - two pizzas are equal if they have the same size and same toppings
# (toppings don’t need to be in the same order)
def __eq__(self, value):
if self.size != value.size:
return False
if self.toppings != value.toppings:
return False
return True
# testing
pie = Pizza()
print(pie)
# Pizza('M', set())
pie.setSize('L')
print(pie.getSize())
# 'L'
pie.addTopping('pepperoni')
pie.addTopping('anchovies')
pie.addTopping('mushrooms')
print(pie)
# Pizza('L',{'anchovies', 'mushrooms', 'pepperoni'})
pie.addTopping('pepperoni')
print(pie)
# Pizza('L',{'anchovies', 'mushrooms', 'pepperoni'})
pie.removeTopping('anchovies')
print(pie)
# Pizza('L',{'mushrooms', 'pepperoni'})
pie.price()
# 16.65
pie2 = Pizza('L', {'mushrooms', 'pepperoni'})
print(pie2)
# Pizza('L',{'mushrooms', 'pepperoni'})
print(pie == pie2)
# True
![2. Write a function orderPizza that allows the user input to build a pizza. It then prints a thank
you message, the cost of the pizza and then returns the Pizza that was built.
>>> orderPizza()
Welcome to Python Pizza!
What size pizza would you like (S,M,L): M
Type topping to add (or Enter to quit): mushroom
Type topping to add (or Enter to quit): onion
Type topping to add (or Enter to quit): garlic
Type topping to add (or Enter to quit):
Thanks for ordering!
Your pizza costs $14.299999999999999
Pizza('M',{'mushroom', ' onion', 'garlic'})
>>> orderPizza()
Welcome to Python Pizza!
What size pizza would you like (S,M,L): L
Type topping to add (or Enter to quit): calamari
Type topping to add (or Enter to quit): garlic
Type topping to add (or Enter to quit):
Thanks for ordering!
Your pizza costs $16.65
Pizza( 'L',{'garlic', 'calamari'})
>>> prorderPizza()
Welcome to Python Pizza!
What size pizza would you like (S,M,L): s
Type topping to add (or Enter to quit):
Thanks for ordering!
Your pizza costs $6.25
>>> P
Pizza('s',set())
>>>](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fc4b85f9b-2931-43f1-91f6-17c8a9947553%2Ff1940fe2-e929-4147-96e0-045d6976dd58%2F6ig6447_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)