Reputation: 41
I am working on a MyProgrammingLab problem that states the following:
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 variables 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 one. 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).
I am testing my code in jGRASP (not a fan, unfortunately required for the course) and the program works as intended, but it seems to terminate early sometimes, and I'm struggling to figure out why, as I can not find any errors in my logic. Here is what I have:
muffins = 5
cupcakes = 6
buyItem = raw_input("")
while buyItem == "m":
if muffins <= 0:
if cupcakes <= 0:
print("All out of stock.")
break
else:
print("Out of stock.")
buyItem = raw_input("")
else:
muffins -= 1
print("muffins: {} cupcakes: {}".format(muffins, cupcakes))
buyItem = raw_input("")
while buyItem == "c":
if cupcakes <= 0:
if muffins <=0:
print("All out of stock.")
break
else:
print("Out of stock.")
buyItem = raw_input("")
else:
cupcakes -= 1
print("muffins: {} cupcakes: {}".format(muffins, cupcakes))
buyItem = raw_input("")
And here is an example of an output that terminates early:
----jGRASP exec: python test.py
m
muffins: 4 cupcakes: 6
m
muffins: 3 cupcakes: 6
m
muffins: 2 cupcakes: 6
c
muffins: 2 cupcakes: 5
c
muffins: 2 cupcakes: 4
c
muffins: 2 cupcakes: 3
m
----jGRASP: operation complete.
However, if I input differently, it works just fine:
----jGRASP exec: python test.py
m
muffins: 4 cupcakes: 6
m
muffins: 3 cupcakes: 6
m
muffins: 2 cupcakes: 6
m
muffins: 1 cupcakes: 6
m
muffins: 0 cupcakes: 6
m
Out of stock.
c
muffins: 0 cupcakes: 5
c
muffins: 0 cupcakes: 4
c
muffins: 0 cupcakes: 3
c
muffins: 0 cupcakes: 2
c
muffins: 0 cupcakes: 1
c
muffins: 0 cupcakes: 0
c
All out of stock.
----jGRASP: operation complete.
I'm not sure what's wrong. Thoughts?
Upvotes: 1
Views: 172
Reputation: 4388
Because once you leave the while "m"
, you can't get back there.
while buyItem == "m":
#do stuff
while buyItem == "c":
#do other stuff
#end program
When you input m -> c -> m
, m != c
, so we leave while buyItem == "c":
and end the program.
We should instead have one loop, and only ask for input once at the end if we have stock, otherwise break out of the loop when entirely out of stock. Additionally, since your assignment says to only print when input is 0
:
muffins = 5
cupcakes = 6
buyItem = raw_input("")
while buyItem in ["m", "c", "0"]:
if buyItem == "m":
if muffins <= 0:
print("Out of stock.")
else:
muffins -= 1
elif buyItem == "c":
if cupcakes <= 0:
print("Out of stock.")
else:
cupcakes -= 1
elif buyItem == "0":
print("muffins: {} cupcakes: {}".format(muffins, cupcakes))
break
else: # input is not "m" or "c" or "0"
print(buyItem.join(" is not a valid input"))
break
if muffins <= 0 and cupcakes <= 0: # we can check this once at the end and break out if we are entirely out of stock
print("All out of stock.")
break
else: # otherwise, if we have some stock, ask for input
buyItem = raw_input("")
Upvotes: 3