Answer to Question #210831 in Python for Jen

Question #210831

You work for a bakery that sells two items: muffins and cupcakes. The number of muffins and cupcakes in your shop at any given time is stored in the <span style="text-decoration: none;">variables</span> muffins and cupcakes, which have been defined for you. 

Write a program that takes strings from standard input indicating what your customers are buying ("muffin" for a muffin, "cupcake" for a cupcake). If they buy a muffin, decrease muffins by one, and if they buy a cupcake, decrease cupcakes by 1. If there is no more of that baked good left, print ("Out of stock"). 

Once you are done selling, input "0", and have the program print out the number of muffins and cupcakes remaining, in the form "muffins: 9 cupcakes: 3" (if there were 9 muffins and 3 cupcakes left, for example).


1
Expert's answer
2021-06-27T14:09:26-0400
muffins = 3
cupcakes = 3
a = input("Enter the item: ")
while a != '0' or (muffins==0 and cupcakes==0):
    if a == 'muffin':
        if muffins>0:
            muffins-=1
        else:
            print("Zero muffins.")
    if a == 'cupcake':
        if cupcakes>0:
            cupcakes-=1
        else:
            print("Zero cupcakes.")
            
    a = input("Enter the item: ")
    
    if muffins==0 and cupcakes==0:
        print("Out of stock")
        break
print("muffins: %d cupcakes: %d" %(muffins, cupcakes))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS