0

So the program works fine in PyCharm (where I created it) and prints the birth year. But when I run the .py file inside cmd and as a stand-alone exe file (made with pyinstaller), I can only get to the point where the age input is provided and then when I press 'Enter' for the birth year to output, the program crashes before that last step. Below is the code:

# Capture name and age
name = input('What is your name? ')
age = int(input('How old are you? '))

# Calculate birth year and output
current_year = 2023
birth_year = current_year - age
print()
print('Hello', name + '!', 'You were born in', birth_year, end='.')

0

1 Answer 1

2

The program isn't crashing, just finishing. Once the user inputs their age, it runs through the last few lines:

current_year = 2023
birth_year = current_year - age
print()
print('Hello', name + '!', 'You were born in', birth_year, end='.')

But since it reaches the end of the file, the program thinks it's done, and closes the window for you, before you can actually see the last print() statement's output. In PyCharm, it'll likely just go back to the terminal, which is why it seems to be working fine there.

If you add input("Press enter to exit") to the end of your code, the program will instead execute the last few lines, then wait for the user to send another line (by pressing enter). Even though this is done using the input() function, the program will just discard the input and exit once it reaches the end of the program.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your detailed answer. Very much appreciated. I was able to fix the issue using the <input("Press enter to exit")> code at the end. Makes it clear why it works in PyCharm but closes before I can see the final line in cmd or the .exe version.
@superior_light No problem, glad I could help! If you could accept my answer that would be great :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.