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

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 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 >>>...
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...
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
In IDLE - Python 3, do the following: 1. Create File --> New 2. Enter the...
In IDLE - Python 3, do the following: 1. Create File --> New 2. Enter the code below in the new file (you may omit the comments, I included them for explanation #Python Code Begin x = int(input("Enter a number: ")) y = int(input("Enter another number: ")) print ("Values before", "x:", x, "y:", y) #add code to swap variables here #you may not use Python libraries or built in swap functions #you must use only the operators you have learned...
Pythons code using idle Write an input function. Then use it to supply the input to...
Pythons code using idle Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even.
Use Visual Python or equivalent, to write a program that allows the user to observe the...
Use Visual Python or equivalent, to write a program that allows the user to observe the following: Damped and forced harmonic oscillators. The program should be user friendly and have default values for the initial velocities, positions, masses, and spring constants as well as damping constants. Values and frequencies for the forced oscillators should also be given. It should allow the user to input values for the velocities, positions, spring constants, and masses. The program should determine automatically the scale...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT