开发者

Using if/elif/else and resp=raw_input - how to respond to part of a user's input?

print "Please Type Something"
resp = raw_input()

if resp *contains* "cuttlefish"
    print  "Response One"
elif resp *contains* "nautilus"
    print "Response Two"
else:
    print "Response Three"

What I need to know is the correct syntax to use instead of the filler contains. So, for example, if the user types "two cuttlefish" then the program should respond by printing "Response One"

I 开发者_JAVA百科did try look for this information in a few tutorials, so if you know of a good one that addresses this I wouldn't mind a pointer in that direction, either :/


Python is quite an easy language, just think english and you might get it correct ;)

if 'cuttlefish' in resp:


in is the operator you are looking for:

if "cuttlefish" in resp:
    print "Response One"
elif "nautilus" in resp:
    print "Response Two"
else:
    print "Response Three"

in is basically an operator that works on sequences like str, tuple, list. x in y checks if x is part of the sequence y. See also 5.6. Sequence Types in the Python Standard Library documentation.

edit

Regarding your second question “Now, how do I change it so that "cuttlefish" is an entry on cuttlefishList, along with several other entries that should also trigger print "Response One"?”.

That's actually a big more complicated, as you cannot check if any of multiple elements are within a sequence with in. But of course there are possibilities to do this. The simples ist to simply chain all the checks together:

if "cuttlefish" in resp or "someotherfish" in resp or "yetanotherfish" in resp:
    print "Response One"

If you have too many different words that would result in that response, then you can also automate that a bit like this:

if any( x in resp for x in ( "cuttlefish", "someotherfish", "yetanotherfish" ) ):
    print "Response One"

That is basically a short way of writing (not literally of course, but the idea is the same):

checkList = []
for x in ( "cuttlefish", "someotherfish", "yetanotherfish" ):
    checkList.append( x in resp )

if True in checkList:
    print "Response One"

So what it does is that it goes through all elements of your tuple and checks for each element x if it is part of the response. It keeps a check list of the results of these checks (in the short form, a generator does this). At the end, you check if True is part of the sequence checkList. If it does, at least one of the fishes were found in the response and you can print the result.

A different way would be to use regular expressions. It might be overkill to use them for a low number of different choices, but on the other hand, you could do great fancy stuff with it.


if "cuttlefish" in resp:
    # found

or

if resp.find("cuttlefish") > 0:
    # found

http://docs.python.org/library/stdtypes.html#str.find


You can use the following syntax:

if 'string' in word:
   #Code

this checks if it is in word, regardless of it is a list or another string. So it would match True if word was 'foostringbar' or ['foo', 'string', 'bar'] Also, instead of printing to tell the user to enter something, try this:

resp = raw_input("Please Type Something\n")

This will produce the same thing, but is much more pythonic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜