If condition after raw input in python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If condition after raw input in python
# 1  
Old 04-03-2016
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.

Code:
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.

Last edited by ctrld; 04-03-2016 at 02:19 AM..
# 2  
Old 04-03-2016
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.
Code:
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...
Code:
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()


Last edited by wisecracker; 04-03-2016 at 06:28 PM.. Reason: See above...
This User Gave Thanks to wisecracker For This Post:
# 3  
Old 04-04-2016
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.

Code:
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()

# 4  
Old 04-04-2016
I haven't done python since 2013 but here goes...
Code:
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.
Code:
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...

Last edited by wisecracker; 04-04-2016 at 02:34 PM.. Reason: See above.
This User Gave Thanks to wisecracker For This Post:
# 5  
Old 04-04-2016
Sweet!! I thought about an exit function but it was not this neat.. this is cool..
# 6  
Old 04-04-2016
If you want a return code here you go...
Code:
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!")

This User Gave Thanks to wisecracker For This Post:
# 7  
Old 04-05-2016
Thanks wisecracker!!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to add +1 to value based on condition in input

In the awk below I am trying to add a | that will adjust $2 in the ouput by adding +1 if the original value from file that was used in $3 had a - in it. Line 3 of file is an example of this. In my current awk I just subtract one but I am not sure how to only apply this to those values without a -.... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Python problem with input

Hello everyone, I have just started python, and there is something that escapes me. I don't understand why my little script doesn't work. When I do, it work: my_string = "bonjour" if len(my_string) < "6": print(my_string + " < 6") else: print(my_string + " > 6") ... (2 Replies)
Discussion started by: Arnaudh78
2 Replies

3. Shell Programming and Scripting

Kbd input processing with python

Hi, I'm new to python, and I work on a little program fpr my rapsberry pi. This scenario here is a little Midi sysex app for my Roland D-50 Synthesizer. With an usb attached number keypad I can type numbers from 0 to 9 and the referenced Midi sysex file would be send out to my Roland D-50... (1 Reply)
Discussion started by: sdohn
1 Replies

4. Shell Programming and Scripting

Reducing the decimal points of numbers (3d coordinates) in a file; how to input data to e.g. Python

I have a file full of coordinates of the form: 37.68899917602539 58.07500076293945 57.79100036621094 The numbers don't always have the same number of decimal points. I need to reduce the decimal points of all the numbers (there are 128 rows of 3 numbers) to 2. I have tried to do this... (2 Replies)
Discussion started by: crunchgargoyle
2 Replies

5. Shell Programming and Scripting

Move input file based on condition

Hello, I have File1 in a directory A, a File2 in a directory B. If the File2 is not empty Then I have to move File1 from directory A to a directory archive Else no action. Is it possible to do this from one command line? Thank you in advance for your answers. Madi (2 Replies)
Discussion started by: AngelMady
2 Replies

6. Shell Programming and Scripting

Change in Input feed based on condition file

Sorry Guys for not being able to explain in one of my earlier post. I am now putting my requirement with the input file and desired output file. In the below input file - Transaction code is at position 31:40. Business code is from position 318:321 TSCM00000005837 ... (7 Replies)
Discussion started by: varunrbs
7 Replies

7. Shell Programming and Scripting

Copy input file based on condition

Hi, I am new to unix shell programming. I want to write a shell script for a functionality existing in mainframe system. I have one file as below as input 123456 &__987 &12yuq abcdef _ referes to blank condition:whenever the input file is having &__ ,it should be replaced... (4 Replies)
Discussion started by: charan0703
4 Replies

8. Shell Programming and Scripting

Awk to print on condition in input file

I need only those records which has $2 equal to "DEF" independent of case (i.e upper or lower) nawk -F"," '$2 ~ //{print $0}' file This returns 3rd record also which i dont want I tried this but this doesnt work as expected. nawk -F"," '$2 == ""{print $0}' file i dont... (3 Replies)
Discussion started by: pinnacle
3 Replies
Login or Register to Ask a Question