473,416 Members | 1,750 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,416 software developers and data experts.

noob question Letters in words?

Alright heres another noob question for everyone. Alright, say I have a
menu like this.

print "1. . .Start"
print "2. . .End"
choice1 = raw_input("> ")

and then I had this to determine what option.
if choice1 in ('1', 'Start', 'start'):
#do first option
if choice1 in ('2', 'End', 'end'):
#do second option

Is there a way (I searched for a module but didnt find one) that I can do
something like this?

if choice1 in ('1', 'S', 's'):
#do first option
if choice1 in ('2', 'E', 'e'):
#do second option
For instance I could type in Stop and would get the first option since it
had an "s" in it?
Anyone heard of any way to do this?

Thanks,
-Ivan

By the way, if anyone gets tired of my persistant noob questions please tell
me I don't want to bother anyone =D

__________________________________________________ _______________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/g...ave/direct/01/

Oct 8 '05 #1
8 2119

Ivan Shevanski wrote:
Alright heres another noob question for everyone. Alright, say I have a
menu like this.

print "1. . .Start"
print "2. . .End"
choice1 = raw_input("> ")

and then I had this to determine what option.
if choice1 in ('1', 'Start', 'start'):
#do first option
if choice1 in ('2', 'End', 'end'):
#do second option

Is there a way (I searched for a module but didnt find one) that I can do
something like this?

if choice1 in ('1', 'S', 's'):
#do first option
if choice1 in ('2', 'E', 'e'):
#do second option
Why not just look at the first letter the user types instead of
the whole string?

if choice1[0] in ('1', 'S', 's'):
#do first option
if choice1[0] in ('2', 'E', 'e'):
#do second option



For instance I could type in Stop and would get the first option since it
had an "s" in it?
Anyone heard of any way to do this?

Thanks,
-Ivan

By the way, if anyone gets tired of my persistant noob questions please tell
me I don't want to bother anyone =D

__________________________________________________ _______________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/g...ave/direct/01/


Oct 8 '05 #2
A string can be thought of as a tuple of characters. Tuples support
membership testing thus...

choice1 = raw_input("> ")

if '1' or 's' or 'S' in choice1:
#do something
elif '2' or 'e' or E' in choice1:
#do something

It doesn't seem to me to be a good idea; If the input is 'Start',
option1 is executed, likewise if the input is 'Stop', or any other
string with 's' in it.

Perhaps a better idea is to present the user with a choice that cannot
be deviated from, along the lines of...

def main():
print "1.\tStart"
print "2.\tSomething Else"
print "3.\tStop"

x = raw_input()
if x is '1': print 'Start'
elif x is '2': print 'Something else'
elif x is '3': print 'End'
else: main()

Oct 8 '05 #3
On Fri, 07 Oct 2005 20:46:39 -0400, Ivan Shevanski wrote:
Alright heres another noob question for everyone. Alright, say I have a
menu like this.

print "1. . .Start"
print "2. . .End"
choice1 = raw_input("> ")

and then I had this to determine what option.
Firstly, you need to decide on what you want to accept. If you really do
want to (I quote from later in your post)
For instance I could type in Stop and would get the first option since
it had an "s" in it?
then you can, but that would be a BAD idea!!!

py> run_dangerous_code()
WARNING! This will erase all your data if you continue!
Are you sure you want to start? Yes/Start/Begin Stop

Starting now... data erased.

Oh yeah, your users will love you for that.

I could handle it like this:

def yesno(s):
"""Returns a three-valued flag based on string s.
The flag is 1 for "go ahead", 0 for "don't go"
and -1 for any other unrecognised response.
Recognises lots of different synonyms for yes and no.
"""
s = s.strip().lower() # case and whitespace doesn't matter
if s in ('1', 'start', 's', 'begin', 'ok', 'okay', 'yes'):
return 1
elif s in ('2', 'end', 'e', 'cancel', 'c', 'stop', 'no'):
return 0
else:
return -1
def query():
print "Start process: 1 start"
print "End process: 2 end"
raw_choice = raw_input("> ")
choice = yesno(raw_choice)
if choice == -1:
print "I'm sorry, I don't understand your response."
return query()
else:
return choice
But that's probably confusing. Why do you need to offer so many ways of
answering the question? Generally, the best way of doing this sort of
thing is:

- give only one way of saying "go ahead", which is usually "yes".
- give only one way of saying "don't go ahead", which is usually "no".
- if the thing you are doing is not dangerous, allow a simple return to
do the same as the default.
- if, and only if, the two choices are unambiguous, allow the first
letter on its own (e.g. "y" or "n") to mean the same as the entire word.

Using this simpler method:

def yesno(s):
s = s.strip().lower()
if s in ('yes', 'y'):
return 1
elif s in ('no', 'n'):
return 0
else:
return -1

def query:
print "Start process? (y/n)"
choice = yesno(raw_choice("> "))
if choice == -1:
print "Please type Yes or No."
return query()
else:
return choice
Hope this is helpful.
--
Steven.

Oct 8 '05 #4
"Ivan Shevanski" <da***********@hotmail.com> writes:
choice1 = raw_input("> ")
choice1 is now the whole string that the user types
Is there a way (I searched for a module but didnt find one) that I can
do something like this?

if choice1 in ('1', 'S', 's'):
#do first option


You'd use choice1[0] to get the first letter of choice1.
Oct 8 '05 #5
On Fri, 07 Oct 2005 18:03:02 -0700, me********@aol.com wrote:
Why not just look at the first letter the user types instead of
the whole string?

if choice1[0] in ('1', 'S', 's'):
#do first option
if choice1[0] in ('2', 'E', 'e'):
#do second option


Is it *really* a good idea if the user types "STOP!!!" and it has the
same effect as if they typed "Start please"?

I've heard of "Do what I mean, not what I said" systems, which are usually
a really bad idea, but this is the first time I've seen a "Do what I don't
mean, not what I said" system.

--
Steven.

Oct 8 '05 #6
(re that earlier thread, I think that everyone that thinks that it's a
good thing that certain Python constructs makes grammatical sense
in english should read the previous post carefully...)

Rob Cowie wrote:
A string can be thought of as a tuple of characters.
footnote: the correct python term is "sequence of characters".
Tuples support membership testing thus...

choice1 = raw_input("> ")

if '1' or 's' or 'S' in choice1:
#do something
elif '2' or 'e' or E' in choice1:
#do something

It doesn't seem to me to be a good idea; If the input is 'Start',
option1 is executed, likewise if the input is 'Stop', or any other
string with 's' in it.
in fact, the first choice is always executed, because

if '1' or 's' or 'S' in choice1:
...

might look as if it meant

if ('1' in choice1) or ('s' in choice1) or ('S' in choice1):
...

but it really means

if ('1') or ('s') or ('S' in choice1):
...

since non-empty strings are true, the '1' will make sure that the entire
expression is always true, no matter what choice1 contains.
Perhaps a better idea is to present the user with a choice that cannot
be deviated from, along the lines of...

def main():
print "1.\tStart"
print "2.\tSomething Else"
print "3.\tStop"

x = raw_input()
if x is '1': print 'Start'
elif x is '2': print 'Something else'
elif x is '3': print 'End'
else: main()


The "is" operator tests for object identity, not equivalence. Nothing
stops a Python implementation from creating *different* string objects
with the same contents, so the above isn't guaranteed to work. (it
does work on current CPython versions, but that's an implementation
optimization, and not something you can rely on).

You should always use "==" to test for equivalence (and you should never
use "is" unless you know exactly what you're doing).

</F>

Oct 8 '05 #7
Well.. that put me in my place!

Fredrik Lundh - I hadn't realised that 'is' does not test for
equivalence. Thanks for the advice.

Oct 8 '05 #8
BTW do yourself a favor and learn to use the cmd module:

2 nice examples:
http://www.eskimo.com/~jet/python/examples/cmd/

it will also give you command completion if your termianl supports it
(most terminals on most linux distros do).

Oct 9 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: r holland | last post by:
My nine word description of the python language.
19
by: Johannes Nix | last post by:
Hello, yesterday I met a cute person (after my dance class) who told me about an interesting experiment regarding cognition. People were told to read a typed text; However, in every word in the...
3
by: Pekka Jarvela | last post by:
I am using Visual Studio C++ .NET and when I try to print words with umlaut letters, for instance printf("Pässinpää-ääliö"); letters with dots over them, äö, will not be printed correctly on...
2
by: Chris Dunaway | last post by:
I was looking over some asp.net code and saw this asp:Image control: <asp:Image ImageUrl="~/images/logo.gif" runat="server" ID="Image1"/> What does the tilde (~) mean in the ImageUrl...
3
by: mark | last post by:
hi guys i need hlp on a c programcounting letters,words,etc .simple. thanks
5
by: Omar | last post by:
Hi all...this is a good group so I'm sure you'll tolerate some more noobish questions... 1) I'm also learning to program flash movies while I learn to do python. How can one implement flash...
6
by: Olagato | last post by:
I need to transform this: <urlset xmlns="http://www.google.com/schemas/sitemap/0.84"> <url> <loc>http://localhost/index.php/index./Paths-for-the-extreme-player</ loc> </url> <url>...
1
by: sumone14 | last post by:
I have to create a program that opens a file and I have to find and show the words that have the most letters. I got the file to open but I can't figure out how to count the letters. I think I have...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.