Python 文件可执行文件错误



我用python为一个游戏做了一个燃料计算器程序,然后我用cx_Freeze编译成.exe。它可以很好地将其转换为.exe,我可以打开可执行文件,但是当脚本与用户交互时,当用户引入请求的信息时,按 Enter 后窗口关闭。这是代码的一部分,在向用户请求一些信息后,程序会进行一些计算,但我认为这无关紧要,因为问题出在输入中。我希望当用户按回车键输入请求的信息时,程序不会关闭。

import sys
COMBUSTIBLE=chr(raw_input("Introduce unidad de combustible: "))
DURACION=chr(raw_input("Introduce unidad de duracion: "))
if COMBUSTIBLE != "litros" and COMBUSTIBLE != "kilos" and DURACION != "vueltas" and DURACION != "tiempo" and DURACION != "km":
    print "Error: Ambos argumentos son invalidos"
    print "Primer argumento debe ser 'litros' o 'kilos'"
    print "Segundo argumento debe ser 'tiempo' o 'vueltas' o 'km'"
    sys.exit(1)
elif COMBUSTIBLE != "litros" and COMBUSTIBLE != "kilos":
    print "Error: Primer argumento invalido"
    print "Primer argumento debe ser 'litros' o 'kilos'"
    sys.exit(2)
elif DURACION != "tiempo" and DURACION != "vueltas" and DURACION != "km":
    print "Error: Segundo argumento invalido"
    print "Segundo argumento debe ser 'tiempo' o 'vueltas' o 'km'"
    sys.exit(3)
else:
    pass
# TIPO 1 - LITROS - VUELTAS 
if COMBUSTIBLE == "l" and DURACION == "v":
    # DATA REQUEST
    RACE_DURATION=int(raw_input("Introduce el total de vueltas de la carrera: "))
    CAR_FUEL=float(raw_input("Introduce los litros totales del coche: "))
    FUEL_PER_LAP=float(raw_input("Introduce el consumo medio en litros por vuelta: "))

程序完成执行后,窗口将立即关闭。因此,如果您希望窗口保持打开状态,您应该删除 sys.exit() 语句并在脚本末尾添加一些内容,例如:

input("Press any key to exit: ") 

在 Python 3 中或

raw_input("Press any key to exit: ") 

在 Python 2 中

相关内容

最新更新