Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit program using the enter key

Tags:

python

Michael Dawson says in his book Python Programming (Third Edition, page 14) that if I enter input("\n\nPress the enter key to exit.") when the user presses the Enter key the program will end.

I have tried this several times and it doesn't happen. I have tried using Python 3.1 and 3.3. Help would be appreciated.

like image 216
user3306860 Avatar asked Jul 19 '26 12:07

user3306860


2 Answers

This is a fairly straightforward concept, do take a look at this example

x = input("Hit Enter to Exit, or Input any other key to continue ")

if not x :
    print("Exiting the Program.")
    exit()

else:
    a = int(input("\nEnter a Number : "))
    b = int(input("Enter another Number : "))
    print("Sum of the two numbers : ", a+b)
like image 140
PreethiSamanthaBennet Avatar answered Jul 22 '26 02:07

PreethiSamanthaBennet


The input() function merely waits for you to enter a line of text (optional) till you press Enter. The sys.exit("some error message") is the correct way to terminate a program. This could be after the line with the input() function.

like image 29
arocks Avatar answered Jul 22 '26 03:07

arocks