我正在为文本冒险游戏创建一个对话树,该游戏使用列表来保存对话选项。当用户从列表中选择一个选项时,它会响应,然后从列表中删除该选项,以便用户无法再次选择它。问题是,一旦我选择了一个选项(循环的第一个实例),第二个选择总是会返回错误。
以下是代码:
x = 1
dec1 = '"Why are you doing this? What do you want?"'
dec2 = '"You keep talking about Thread. What is it?"'
dec3 = '"Can you help me?"'
dec4 = 'Say nothing.'
decision = [dec1, dec2, dec3, dec4]
while x == 1:
for i in decision:
print '%d. %s' % (decision.index(i) + 1, i)
current_input = input.return_input(charstate)
if isinstance(current_input, int):
if int(current_input) > len(decision) or int(current_input) < 1:
print '\n"I don\'t understand, %s."' % charstate.charname
continue
elif int(current_input) == decision.index(dec1) + 1:
print 'text'
decision.remove(dec1)
elif int(current_input) == decision.index(dec2) + 1:
print 'text'
decision.remove(dec2)
elif int(current_input) == decision.index(dec3) + 1:
print 'text'
decision.remove(dec3)
elif int(current_input) == 4:
print 'text'
x += 1
else:
exit(1)
else:
print '\n"I don\'t understand, %s."' % charstate.charname
continue
问题似乎是在我选择一个选项后,if条件会返回错误,因为decision.index(dec#)不再存在。至少,这是我能够弄清楚的。具体错误是“ValueError:x不在列表中”。我正在试图找到一种方法来设置它,以便不会发生这种情况。
我唯一的想法就是为每个选项创建单独的try ... except语句,这看起来非常不优雅和臃肿。我必须要有一些简单的东西。
谢谢大家!
edit:input.return_input(charstate)是一个只返回raw_input的函数。 “charstate”是一个保存当前角色信息和选择历史的对象 - 它在事件和场景之间传递。
答案 0 :(得分:0)
通过索引从数组中删除,我相信这将为您提供所需的功能
dec1 = '"Why are you doing this? What do you want?"'
dec2 = '"You keep talking about Thread. What is it?"'
dec3 = '"Can you help me?"'
dec4 = 'Say nothing.'
decision = [[dec1,'text1'], [dec2,'text2'], [dec3,'text3'], [dec4,'text4']]
while True:
for i in decision:
print '%d. %s' % (decision.index(i) + 1, i[0])
current_input = input.return_input(charstate)
if isinstance(current_input, int):
if int(current_input) > len(decision) or int(current_input) < 1:
print '\n"I don\'t understand, %s."' % charstate.charname
continue
elif int(current_input) in xrange(1,len(decision)):
print '%s' % (decision[int(current_input)-1][1])
del decision[int(current_input)-1]
elif int(current_input) == 4:
print 'text'
break
else:
exit(1)
else:
print '\n"I don\'t understand, %s."' % charstate.charname
continue
答案 1 :(得分:0)
好的,我实际上已经想出了如何做我需要做的事情。假名的答案对我来说是一个很好的第一步,但后来我意识到我需要根据答案实际运行单独的代码,而不仅仅是打印不同的代码。
我重新构建了代码,因此我不需要调用索引来确定选择了哪个答案。这实际上是一个相对简单的修复,一旦我了解它。
dec1 = '"Why are you doing this? What do you want?"'
dec2 = '"You keep talking about Thread. What is it?"'
dec3 = '"Can you help me?"'
dec4 = 'Say nothing.'
decision = [dec1, dec2, dec3, dec4]
while True:
for i in decision:
print '%d. %s' % (decision.index(i) + 1, i)
current_input = input.return_input(charstate)
if isinstance(current_input, int):
current_input = int(current_input)
if current_input - 1 in xrange(0, len(decision)):
response = decision[current_input - 1]
elif current_input - 1 not in xrange(0, len(decision)):
print '\n"I don\'t understand, %s."\n' % charstate.charname
continue
else:
exit(1)
else:
print '\n"I don\'t understand, %s."\n' % charstate.charname
continue
if response == dec1:
print 'dec1'
elif response == dec2:
print 'dec2'
elif response == dec3:
print 'dec3'
elif response == dec4:
print 'dec4'
break
else:
exit(1)
del decision[current_input - 1]