Windows Python 3.7 在 .py 文件完成之前关闭 运行

Windows Python 3.7 closing before .py file is done running

我在 Spyder 中写了一个 python 脚本,并试图在我的机器上 运行 它但是 Python 3.7 on windows 提前关闭它。

我的代码是:

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

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

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

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('\nPress "ENTER" to exit')

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

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