【问题标题】:Program is terminating early sooner than I expected计划提前终止,比我预期的要早
【发布时间】:2018-03-30 00:07:30
【问题描述】:

我正在处理一个 MyProgrammingLab 问题,该问题陈述如下:

您在一家出售两种商品的面包店工作:松饼和纸杯蛋糕。在任何给定时间,您商店中的松饼和纸杯蛋糕的数量都存储在为您定义的变量松饼和纸杯蛋糕中。 编写一个程序,从标准输入中获取字符串,指示您的客户正在购买什么(“松饼”代表松饼,“纸杯蛋糕”代表纸杯蛋糕)。如果他们买松饼,减少一个松饼,如果他们买一个纸杯蛋糕,减少一个纸杯蛋糕。如果没有更多的烘焙食品,请打印(“缺货”)。

完成销售后,输入“0”,然后让程序打印出剩余的松饼和纸杯蛋糕的数量,格式为“松饼:9 个纸杯蛋糕:3”(如果剩下 9 个松饼和 3 个纸杯蛋糕,例如)。

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

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.

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

【问题讨论】:

  • 你只检查松饼,然后纸杯蛋糕,然后你什么都不检查。浏览代码,它会很有意义。
  • 嗨@Tree,查看python debugger,这是一种在您放置import pdb; pdb.set_trace() 的任何地方停止Python 脚本的方法,然后逐行执行,显示变量值,进入子功能等,以尝试发现问题的根本原因。调试是每个编码人员在他们的工具包中应该具备的一项宝贵技能。大多数 IDE,如 PyCharm、Spyder、Visual Studio w/PythonTools 和 Eclipse+PyDev/LiClipse 都有一个内置的图形调试器。希望这对您下次有帮助。
  • 什么样的疯子买了纸杯蛋糕会去买松饼?
  • @Tree 欢迎来到 StackOverflow。既然你是新人,你读过What should I do when someone answers my question?

标签: python


【解决方案1】:

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

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

#end program

当你输入m -&gt; c -&gt; mm != 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("")

【讨论】:

  • 谢谢!此外,我在测试时包含了打印语句,因此我可以看到变量。有没有更好或更简单的方法来做到这一点?
  • 调试器是程序员的天赐之物。它可以让您查看程序状态,例如程序在执行期间的位置、哪些变量具有哪些值等,而不仅仅是盲目地打印内容。我个人使用过 PyCharm 及其美妙的、免费的。否则,您所做的一切都很好,因为它至少为您(和我)提供了一个确定问题的起点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2020-02-19
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 2021-01-09
相关资源
最近更新 更多