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

Need a function. Any built-in function or module for this?

P: n/a
wcc
Hello group,

I need to write a function to get input from user. The prompt will be
like this:
Specify direction[Left/Right/Up/Down] or <Left>:

And if user type "L" or <ENTER>, the function will return "Left", if
user type "R", the function will return "Right", etc..

My quick search in python reference did not yield valid result. Any
pointer?

Thanks for your help.

-wcc

Oct 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"wcc" <wc****@gmail.comwrites:
Specify direction[Left/Right/Up/Down] or <Left>:

And if user type "L" or <ENTER>, the function will return "Left", if
user type "R", the function will return "Right", etc..
Hmm:

def getchoice(prompt, choices, default):
"""prompt is a format string with a %s where the list of choices
should go, and another %s where the default should go;
choices is a list of choices; default is the default choice"""
assert default in choices
prompt %= ('/'.join(choices), default)

while True:
c = raw_input (prompt)
if not c:
return default
a = [x for x in choices if x.startswith(c)]
if len(a) == 0:
print 'Please choose one of', '/'.join(choices)
elif len(a) 1:
print 'ambiguous, enter a unique prefix'
else:
return a[0]

def test():
print getchoice('Specify direction %s or <%s>: ',
("Left","Lexy","Right","Up","Down"), "Left")

test()
Oct 15 '06 #2

P: n/a
wcc
Thanks you very much Paul. I think I have a little more homework to
do.
1. The upper case letter may not be the first letter in each word.
2. There may be more than one (continous) upper case letters in one
word.
I just wanted to find out if there is something already there in python
built-in functions, modules. Thanks again.

Oct 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.