[Python-il] py module for prompting user to select from list

Amit Dor-Shifer amitds at oversi.com
Tue Jun 23 14:55:10 IDT 2009


Thanks to all. I didn't know enumerate() and would use it.
Amit

Beni Cherniavsky wrote:
> On Tue, Jun 23, 2009 at 11:19, Amit Dor-Shifer<amitds at oversi.com> wrote:
>   
>> Hi.
>> Is there some common implementation in python for displaying to user an
>> enumerated  list of items, and then prompt her to select one by
>> selecting the item's number?
>>
>>     
> Sounds simple enough to roll your own:
>
> def prompt_choice(items):
>     for n, item in enumerate(items):
>         print '[%s] %s' % (n + 1, item)
>     while True:
>         response = raw_input("Please select 1..%s and press Enter: " % (n + 1))
>         if response.isdigit():
>             choice = int(response) - 1
>             if 0 <= choice < n + 1:
>                 return items[choice]
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://hamakor.org.il/pipermail/python-il/attachments/20090623/41cae713/attachment.htm 


More information about the Python-il mailing list