Question

In: Computer Science

Python 2. Please ensure you run the tests in idle Write a function orderPizza that allows...

Python 2. Please ensure you run the tests in idle

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'})

>>> p=orderPizza()

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())

>>>

please run this tests in idle to ensure it is correct

##### orderPizza #####

>>> orderPizza()==Pizza('M',{'garlic', 'onion', 'mushroom'})

Welcome to Python Pizza!

What size pizza would you like (S,M,L): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Thanks for ordering!

Your pizza costs $14.299999999999999

True

>>> orderPizza()==Pizza('L',{'calamari', 'garlic'})

Welcome to Python Pizza!

What size pizza would you like (S,M,L): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Type topping to add (or Enter to quit): Thanks for ordering!

Your pizza costs $16.65

True

>>> orderPizza()==Pizza('S',set())

Welcome to Python Pizza!

What size pizza would you like (S,M,L): Type topping to add (or Enter to quit): Thanks for ordering!

Your pizza costs $6.25

True

##### stdin back #####

put stdin back to original, again, shouldnt cause error

>>> sys.stdin = si # return stdin

Solutions

Expert Solution

class Pizza:

def __init__(self, builder):

self.builder = builder

self.pizzaObj = self.__ComputeCost__(builder)

def getPizzaObj(self):

return self.pizzaObj

def __ComputeCost__(self, builder):

cost = builder.pizzaBase[1]

items = []

if builder.mushroom[0]:

cost = cost + builder.mushroom[1]

items.append("mushroom")

if builder.garlic[0]:

cost = cost + builder.garlic[1]

items.append("garlic")

if builder.onion[0]:

cost = cost + builder.onion[1]

items.append("onion")

if builder.calamari[0]:

cost = cost + builder.calamari[1]

items.append("calamari")

return (cost, items)

class PizzaBuilder:

def __init__(self, size, basePrice):

self.pizzaBase = (size, basePrice) # 10 is the base cost

self.mushroom, self.onion = (False, 0), (False, 0)

self.garlic, self.calamari = (False, 0), (False, 0)

def AddMushroom(self):

self.mushroom = (True, 2)

return self

def AddGarlic(self):

self.garlic = (True, 2)

return self

def AddOnion(self):

self.onion = (True, 1)

return self

def AddCalamari(self):

self.calamari = (True, 3)

return self

def Build(self):

return Pizza(self)

if __name__ == '__main__':

pizzaSize = 'start'

while pizzaSize != "q":

print("Welcome To Python Pizza")

pizzaSize = input("Please Select the Size of the Pizza (S, M, L):").strip()

if pizzaSize == 'S' or pizzaSize == 's':

cost = 10

elif pizzaSize == 'M' or pizzaSize=='m':

cost = 15

elif pizzaSize == 'L' or pizzaSize == 'l':

cost = 20

else:

print("Invalid Choice", pizzaSize, " " , "(Available Sizes are: S, M, L) Try Again. To Quit (Type q to quit" )

continue

pizzaObj = PizzaBuilder(pizzaSize, cost)

topping = -1

while topping !=0:

topping = int(input("Please Choose the Topping Available (Mushroom(1), Garlic(2), Onion(3), Calamari(4), To Place Order(0): ").strip())

if topping == 1:

pizzaObj = pizzaObj.AddMushroom()

elif topping == 2:

pizzaObj = pizzaObj.AddGarlic()

elif topping == 3:

pizzaObj = pizzaObj.AddOnion()

elif topping == 4:

pizzaObj = pizzaObj.AddCalamari()

pizza = pizzaObj.Build()

obj = pizza.getPizzaObj()

print("Your Pizza Costs: ", obj[0] )

print(f'Your Pizza({pizzaSize, obj[1]})')


Related Solutions

Please use python and run in IDLE Question 1 Write functions that do the following: i)...
Please use python and run in IDLE Question 1 Write functions that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both. Question 2 Write functions: i) One that prompts a user for 2 numbers. ii)...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
run in python IDLE 3.9.0 i) One that prompts a user for 2 numbers. ii) Adds...
run in python IDLE 3.9.0 i) One that prompts a user for 2 numbers. ii) Adds the two numbers if they are even iii) Multiplies the two numbers if they are odd iv) Displays the appropriate output to the user You are writing 4 functions and calling them to test functionality.
In python idle 3.9.0 write a function that: i) will count all lower case, upper case,...
In python idle 3.9.0 write a function that: i) will count all lower case, upper case, integers, and special symbols from a given string. The input string is provided by the user. ii) will check if a sting is a palindrome. User supplies the input string.
Write in Python and as 2 seperate programs Write a program that allows the user to...
Write in Python and as 2 seperate programs Write a program that allows the user to enter the total rainfall for each of 12 months into a LIST. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January 7.9 inches February 10.1 inches March 3.4 inches April 6.7 inches May 8.9 inches June 9.4 inches July 5.9 inches August 4.1 inches September...
In Python iOverlap (a1, a2, b1, b2) Write the function iOverlap that tests whether 2 closed...
In Python iOverlap (a1, a2, b1, b2) Write the function iOverlap that tests whether 2 closed intervals overlap. It takes 4 numbers (ints or floats) a1, a2, b1, b2 that describe the two closed intervals [a1,a2] and [b1,b2] of the real number line, and returns True if these two closed intervals overlap (even if at only one point) and False otherwise. If a1>a2, then the interval [a1,a2] is empty. If b1>b2, then the interval [b1,b2] is empty. Both intervals are...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
PleaSe write a GENERATOR function in Python: how to write it with yield ? XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Thanks...
PleaSe write a GENERATOR function in Python: how to write it with yield ? XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Thanks in advance Write /the/ generator /function/ genAccum(seq, fn), which takes in an iterable and a function fn and yields each accumulated value from applying fn to the running total and the next element. (Note: to convert an iterable into an iterator you use iter(iterable)) ''' >>> add = lambda x, y: x + y >>> mul = lambda x, y: x * y >>>...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a series of string values into a list. When the user enters the string ‘done’, stop prompting for values. Once the user is done entering strings, create a new list containing a palindrome by combining the original list with the content of the original list in a reversed order. Sample interaction: Enter string: My Enter string: name Enter string: is Enter string: Sue Enter string:...
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT