0

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>>> '))
4

1 回答 1

0

该回溯是非常无信息的。相关的异常是什么?

一个明显的可能问题来源是您正在重用 ans 和 split_ans 变量。由于您正在对 split_ans 进行迭代,因此您可能会遇到问题,因为您试图在执行中期更改它,这是一个禁忌。

另一件事是-您确定要休息而不是继续吗?你想在第一次命中后逃避解析循环吗?

于 2013-03-23T17:15:45.553 回答