Python文件executab

2024-09-28 21:59:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python为一个游戏编写了一个燃料计算器程序,然后用cx\u 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: "))

Tags: and用户inputrawsysdeprinttiempo
1条回答
网友
1楼 · 发布于 2024-09-28 21:59:10

程序执行完毕后,窗口将立即关闭。所以如果你想让窗户开着你应该把它移开系统出口()语句,并在脚本末尾添加以下内容:

input("Press any key to exit: ") 

在Python 3或

raw_input("Press any key to exit: ") 

在Python 2中

相关问题 更多 >