473,382 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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
2 867
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex Elbert | last post by:
Hi I have built dynamic HTMLTable. Now I want to attach it directly to the Email Body - it is already built, so why not to use a ready table. However, I cannot find the way of getting plain HTML...
39
by: Scotter | last post by:
Okay I think my title line was worded misleadingly. So here goes again. I've got quite 20 identical MDB files running on an IIS5 server. From time to time I need to go into various tables and add...
1
by: William | last post by:
Looking for a pre built dotnet corporate or small business website template.
1
by: William | last post by:
Looking for a pre built dotnet corporate or small business website template.
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.