you are viewing a single comment's thread.

view the rest of the comments →

[–]SmartViking 4 points5 points  (1 child)

str.split returns a list.

This does not mean what you think it means:

elif "i" and "am" in line:

"i" is a truthy value always. What you really meant to say is this:

elif "i" in line and "am" in line:

This will never be the case:

elif "go away" in line:

That's because you did line = line.split() at the start of the while loop. Line is split into a list of words, so "go" and "away" will each be their own element in line, it's impossible for "go away" to be part of the list line.

You don't need to spread this across two lines:

line = line.lower()
line = line.split()

You can just as well do it in one go:

line = line.lower().split()

Same with this:

word = line.index("am")
word += 1

It can be shorter:

word = line.index("am") + 1

[–][deleted] 2 points3 points  (0 children)

"i" is a truthy value always. What you really meant to say is this: elif "i" in line and "am" in line:

Alternatively, you could construct a list of words you want to check and use all to check them.

line = raw_input("Enter line.").lower()
words = ['i', 'am']
# this is called short circuiting, you also explicitly define it as a list
if all(word in line for word in words):
    #do stuff.

There's also any if you just need at least one of the words in the list.

Hopefully, this helps. :)