【问题标题】:Windows Python 3.7 closing before .py file is done running [duplicate]在 .py 文件完成运行之前关闭 Windows Python 3.7 [重复]
【发布时间】:2021-09-14 16:55:32
【问题描述】:

我在 Spyder 中编写了一个 python 脚本,并试图在我的机器上运行它,但 Windows 上的 Python 3.7 将它关闭得很早。

我的代码是:

print('Campus Pizza Ordering Program\n')

crust = input('What crust? Regular or Wheat? ') 
num_toppings = int(input('How many toppings (please enter a whole number)? '))
price = format((7 + num_toppings * 0.75),".2f") #looked up how to round to 2 decimals

print('\nYou have a '+crust+' pizza with '+str(num_toppings)+' toppings. ' +
      'The final price of the pizza is $'+str(price)+".")

程序在最后的打印语句执行之前关闭。为什么?

【问题讨论】:

  • 你是如何运行程序的?点击它?使用 cmd 提示符?

标签: python spyder


【解决方案1】:

我假设您正在通过“运行 python 文件”(python 终端)运行此脚本。这将在您完成后直接关闭窗口。绕过这个的方法是在底部添加一个空输入。

print('Campus Pizza Ordering Program\n')

crust = input('What crust? Regular or Wheat? ') 
num_toppings = int(input('How many toppings (please enter a whole number)? '))
price = format((7 + num_toppings * 0.75),".2f") #looked up how to round to 2 decimals

print('\nYou have a '+crust+' pizza with '+str(num_toppings)+' toppings. ' +
      'The final price of the pizza is $'+str(price)+".")
input()

【讨论】:

  • 您也可以通过命令行启动脚本,避免添加input()
【解决方案2】:

这里发生的最有可能是程序打印了最后一行,然后立即终止,导致您看不到打印。

尝试将其添加到脚本的末尾:

input('\nPress "ENTER" to exit')

【讨论】:

    【解决方案3】:

    确保 spyder 是最新的,因为如果它使用过时的版本,它可能会关闭

    【讨论】:

      【解决方案4】:

      考虑导入时间并在程序结束时使用time.sleep(5),以便查看输出是否打印。

      【讨论】:

        猜你喜欢
        • 2013-01-11
        • 1970-01-01
        • 2012-03-10
        • 2014-08-12
        • 2021-03-28
        • 1970-01-01
        • 1970-01-01
        • 2017-07-30
        • 2021-11-23
        相关资源
        最近更新 更多