Search code examples
pythonexecutablecx-freeze

Python file executable error


I did a fuel calculator program for a game with python and then I compiled to .exe with cx_Freeze. It converts it well to .exe and I can open the executable but when the script interacts with the user the window closes after pressing enter when the user introduce the requested information. This is one part of the code, after requesting some information to the user the program does some calculations but I think it's irrelevant because the problem is in the input. I want that the program doesn't close when the user press enter in the input of info requested.

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: "))

Solution

  • The window will be closed right after your proggram finished executing. So if you want the window stays open you should remove sys.exit() statements and add something at the end of your script like:

    input("Press any key to exit: ") 
    

    in Python 3 or

    raw_input("Press any key to exit: ") 
    

    in Python 2