If condition after raw input in python

Hi,

I have a python script which is completely interactive. I have almost 10+ raw input statements like below which are collected from user one by one.

req_number = raw_input("Please enter request number or Q to Quit: ")
if (req_number in ['Q', 'q', '']):
    sys.exit()

Is it possible for a more efficient way to provide the option to quit in these types of raw inputs without adding too many lines of code.

I am not sure how you are going to shorten any prompts and also if this is what you are after:-
It looks like you are using pyhton2.6.x or higher...
OSX 10.7.5, default bash terminal using default python version.

Last login: Sun Apr  3 18:15:03 on ttys000
AMIGA:barrywalker~> python
Python 2.7.10 (default, Oct 23 2015, 18:05:06) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x,y,z=raw_input()
12q
>>> print(x)
1
>>> print(y)
2
>>> print(z)
q
>>> exit()
AMIGA:barrywalker~> _

Ignore the paretheses in the "print" STATEMENTS, believe it or not this works right back to python Version 1.4.0...

EDIT:
I misinterpreted your post...

You could try intercepting SIGINT, Ctrl-C, and exit that way...

try:
        # some code here <pass>
except KeyboardInterrupt:
        # Quit the code for versions 2.5.x and lower.
        sys.exit()
        # Or for versions 2.6.x and above. The 'sys' module is not needed to exit here...
        exit()
1 Like

Thanks wisecracker..

The sys module not needed for 2.6 above is a new learning for me.

I am already having a KeyboardInterrupt check in my code.

What I am trying to achieve here is to reduce my repeated lines of code and at same time I don't want to loose the functionality of quitting the program on the input "q".

That is , I don't want user to input everything and then check for q value to exit.

req_number = raw_input("Please enter request number or Q to Quit: ")
if (req_number in ['Q', 'q', '']):
    exit()
name = raw_input("Please enter you name or Q to Quit: ")
if (name in ['Q', 'q', '']):
    exit()

phone = raw_input("Please enter phone number or Q to Quit: ")
if (phone in ['Q', 'q', '']):
    exit()

email = raw_input("Please enter email id  or Q to Quit: ")
if (email in ['Q', 'q', '']):
    exit()

request_code = raw_input("Please enter your request code or Q to Quit: ")
if (request_code in ['Q', 'q', '']):
    exit()

I haven't done python since 2013 but here goes...

def EXIT(QUIT = ""):
	if (QUIT == "q" or QUIT == "Q" or QUIT == ""): exit()

print("Enter 'Q', 'q' or RETURN to QUIT at any time...")
name = raw_input("Enter your name: "); EXIT(name)
date = raw_input("Enter your date of birth: "); EXIT(date)
address = raw_input("Enter your address: "); EXIT(address)
print("You are here!")

Results on OSX 10.7.5, default bash terminal running python 2.7.x.

Last login: Mon Apr  4 17:29:34 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Python
AMIGA:barrywalker~/Desktop/Code/Python> python EXIT.py
Enter 'Q', 'q' or RETURN to QUIT at any time...
Enter your name: Q
AMIGA:barrywalker~/Desktop/Code/Python> python EXIT.py
Enter 'Q', 'q' or RETURN to QUIT at any time...
Enter your name: barry
Enter your date of birth: q
AMIGA:barrywalker~/Desktop/Code/Python> python EXIT.py
Enter 'Q', 'q' or RETURN to QUIT at any time...
Enter your name: barry
Enter your date of birth: today
Enter your address: 
AMIGA:barrywalker~/Desktop/Code/Python> python EXIT.py
Enter 'Q', 'q' or RETURN to QUIT at any time...
Enter your name: barry
Enter your date of birth: today
Enter your address: somewhere
You are here!
AMIGA:barrywalker~/Desktop/Code/Python>  _

EDIT:
Added "Q" and "" for fullness...

1 Like

Sweet!! I thought about an exit function but it was not this neat.. this is cool..

If you want a return code here you go...

def EXIT(QUIT = "", RETCODE = 0):
	if (QUIT == "q" or QUIT == "Q" or QUIT == ""):
		exit(RETCODE)

print("Enter 'Q', 'q' or RETURN to QUIT at any time...")
name = raw_input("Enter your name: "); EXIT(name, 3)
date = raw_input("Enter your date of birth: "); EXIT(date, 2)
address = raw_input("Enter your address: "); EXIT(address, 1)
print("You are here!")
1 Like

Thanks wisecracker!!