By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

python interactive scripts?

P: 11
Hello!

I wanna create a script that goes through a file, and changes whatever relevant fields. This part isn't problematic, but I've decided to add another feature to it - instead of just changing the field, I want to ask the user if he's interested in doing so before every change takes place.
Is there an option in python to communicate with the user while running?

Thanks very much!
Aug 16 '07 #1
Share this Question
Share on Google+
4 Replies


ilikepython
Expert 100+
P: 844
Hello!

I wanna create a script that goes through a file, and changes whatever relevant fields. This part isn't problematic, but I've decided to add another feature to it - instead of just changing the field, I want to ask the user if he's interested in doing so before every change takes place.
Is there an option in python to communicate with the user while running?

Thanks very much!
Yes, to get input from a user, use the raw_input() or input() functions.
Expand|Select|Wrap|Line Numbers
  1. response = raw_input("Do you want to change blah blah blah: ")
  2.  
  3. if response in ' Yy':
  4.     print "Changing"
  5. else:
  6.     print "Skipping"
  7.  
Aug 16 '07 #2

P: 11
Yes, to get input from a user, use the raw_input() or input() functions.
Expand|Select|Wrap|Line Numbers
  1. response = raw_input("Do you want to change blah blah blah: ")
  2.  
  3. if response in ' Yy':
  4.     print "Changing"
  5. else:
  6.     print "Skipping"
  7.  

That absolutely great! I saw the C-Shell equivalent... (:
Will it be correct (syntax-wise) to insert the whole thing in a "while" loop, until I get "y" or "n" from the user?


Thanks again!
Aug 16 '07 #3

ilikepython
Expert 100+
P: 844
That absolutely great! I saw the C-Shell equivalent... (:
Will it be correct (syntax-wise) to insert the whole thing in a "while" loop, until I get "y" or "n" from the user?


Thanks again!
Do you mean something like this?:
Expand|Select|Wrap|Line Numbers
  1. while 1:
  2.     response = raw_input("Enter input: ")
  3.  
  4.     if response in 'YynN':   # keep going if not 'y' or 'n'
  5.         break
Aug 16 '07 #4

P: 11
I think this is just what I mean, will put it to test very soon.
Thanks a lot!
Aug 16 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.