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

noob question Letters in words?

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
(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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.