noob question Letters in words?

I

Ivan Shevanski

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/go/onm00200471ave/direct/01/
 
M

mensanator

Ivan said:
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
 
R

Rob Cowie

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()
 
P

Paul Rubin

Ivan Shevanski said:
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.
 
S

Steven D'Aprano

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
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.
 
S

Steven D'Aprano

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.
 
F

Fredrik Lundh

(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 said:
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>
 
R

Rob Cowie

Well.. that put me in my place!

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top