Need a function. Any built-in function or module for this?
Paul Rubin
http
Sat Oct 14 21:00:16 EDT 2006
"wcc" <wccppp at gmail.com> writes:
> 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()
More information about the Python-list
mailing list