I wrote the following. My goal was to try and create a robust way for the code to read sentences for a text-adventure game. I made lists (h_answer1 and 2
) populated with the keywords I wanted the game to recognize, and then I asked for user input in the mansr
function (mansr
takes its argument and makes it lower case), and then I split that input into a list called split_ans
.
This works about half the time. But if I input certain phrases, like "I believe I will search," it throws me through the else statement, even though "search" appears in my sentence.
If I understand correctly, the for-loop is setting check
equal to each string in the split_ans
list, and then the if-statement is checking to see if that particular check
matches anything in the h_answer
lists. So why would python go to the else statement when the if condition has been met?
def some_function():
print "some stuff"
ans = mansr(raw_input())
split_ans = ans.split(' ')
h_answer1 = ['walk', 'run', 'go']
h_answer2 = ['search', 'look']
for check in split_ans:
if check in h_answer1:
print "Some stuff"
break
elif check in h_answer2:
print "Some stuff"
ans = mansr(raw_input(' '))
split_ans = ans.split(' ')
<section omitted, it's a nested for-loop>
else:
print "I don't understand that input."
some_function()
The traceback doesn't reveal much (some_function
was named long_hallway
, edited above in order to be more generic):
File "test06.py", line 171, in <module> start()
File "test06.py", line 92, in start long_hallway()
File "test06.py", line 59, in long_hallway long_hallway()
File "test06.py", line 59, in long_hallway long_hallway()
File "test06.py", line 13, in long_hallway
ans = mansr(raw_input('\n>>> '))