[Tutor] Not to bad,not to good!how to tell apart!!
Gregor Lingl
glingl@aon.at
Sun Jul 27 13:05:03 2003
Paul Hickey schrieb:
> newbie help needed!!
>
> This is what i've got i could do with some help, thanks!
>
> s = raw_input ("How are you?")
> if s.find('fine')>=0:
> print "Im glad that your good!'
> elif s.find('OK')>=0:
> print "Happy to hear your OK!"
> elif s.find('bad')>=0:
> print "Hey look on the bright side, coud be worse!'
> elif s.find('not too bad')>=0:
> print " im glad to hear your good!"
>
> The last line don't work!!! Help.
>
Hi, Paul,
in any if-elif-elif-... statement exactly one branch will be
executed. In your case if s.find('not too bad')>=0 is True, then also
s.find('bad')>=0 is true, so the corresponding branch will be
executed and "Hey, look at ..." will be printed.
If you exchanged this last two branches, the statement will
work as desired.
> Also can i 'bunch' several words together like,
> elif s.find('fine' or 'good' or 'OK' )>=0:
> print "Glad that thinigs are going well!"
>
This is possible, but for the Python interpreter:
>>> 'fine' or 'good' or 'OK'
'fine'
The reason for this is a bit sophisticated, but for now you should
keep in mind that or-ing (and and-ing) expressions may have surprising
results
for you. (If you like to read about this, look at
http://www.python.org/doc/current/lib/truth.html and
http://www.python.org/doc/current/lib/boolean.html )
However this is not the case if the expressions are themselves boolen ones.
So the following will work:
elif s.find('fine'')>=0 or s.find('good')>=0 or s.find('OK')>=0:
print "etc ...."
> This line dont work either!!!
> I've tried to play with this but ot no avail
>
> There's also the thing about upper and lower case is that a major
> thing or will i forget about that for the time being.
>
To manage this you have to use the string-methods lower() and
upper():
>>> s = "I'm ok"
>>> s.find('OK')
-1
>>> s.upper().find('OK')
4
>>> s.find('ok')
4
>>> t = "I'm OK"
>>> t.find("ok")
-1
>>> t.lower().find("ok")
4
>>>
> Or ?? Am i going about this the right way, if i can 'bunch' several
> key words in the same statement the i could save a lot of typing,
> however is there a better method that a newbie might have a hope of
> understanding.
> Help with ANY part of this would be great.
> Thanks for your time, hope im not asking for too much!
Certainly not. Look on the bright side and feel free to ask more ...
Gregor
> Paul