gpt4 book ai didi

python - 计划终止的时间比我预期的要早

转载 作者:行者123 更新时间:2023-11-30 22:17:42 27 4
gpt4 key购买 nike

我正在解决一个 MyProgrammingLab 问题,其内容如下:

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

我正在 jGRASP 中测试我的代码(不是粉丝,不幸的是类(class)需要),程序按预期工作,但有时它似乎提前终止,我正在努力找出原因,因为我不能找出我的逻辑中的任何错误。这是我所拥有的:

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

这是提前终止的输出示例:

----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.

但是,如果我输入不同的内容,它就可以正常工作:

----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.

我不确定出了什么问题。想法?

最佳答案

因为一旦你离开while "m",你就无法再回到那里。

while buyItem == "m":
#do stuff
while buyItem == "c":
#do other stuff

#end program

当您输入m -> c -> m时,m != c,所以我们离开while buyItem == "c": 并结束程序。

我们应该有一个循环,如果有库存,则在最后只要求输入一次,否则在完全缺货时跳出循环。此外,由于您的作业规定仅在输入为 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("")

关于python - 计划终止的时间比我预期的要早,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49559947/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com