use*_*375 2 python boolean tuples transitions
为什么是inSetStates,inInputAlph并isCorrectDirection评估变量False在下面的代码:
class POC(object):
  def __init__(self):
    self.__set_states = (1,2,3,4,5)
    self.__input_alph = ('a','b')
    self.__directions = ('i','d')
  def enterTransition(self):
    while True:
      print "enter transition tuple format:"
      trans = raw_input(" Example (1,'a',2,'b','d') : ")
      inSetStates = (trans[0] in self.__set_states) and (trans[2] in self.__set_states)
      inInputAlph = (trans[1]in self.__input_alph) and (trans[3] in self.__input_alph)
      isCorrectDirection = (trans[4].lower() in self.__directions) or (trans[4].lower() in self.__directions)
      if (inSetStates and inInputAlph and isCorrectDirection):
        return trans
        break
      else:
        print "ERROR: Something is wrong"
poc = POC()
poc.enterTransition()
调试器显示我的价值three是False与元组(1, 'a', 2, 'b', 'd')和:
inInputAlph = False
inSetStates = False
isCorrectDirection = False
self = <__main__.POC object at 0x1860690>
trans = "(1,\'a\',2,\'b\',\'i\')"
另外,我不知道这些反斜杠是什么.
trans是一个字符串,而不是一个元组.字符串是可转位太多,所以trans[1]是则串 '1 "(第1位的字符).
您需要先将输入转换为元组.一个简单的方法是使用该ast.literal_eval()函数:
 >>> import ast
 >>> ast.literal_eval("(1,\'a\',2,\'b\',\'i\')")
 (1, 'a', 2, 'b', 'i')
该.literal_eval()函数将其输入解释为python文字,并将尝试返回与该输入匹配的python值.
| 归档时间: | 
 | 
| 查看次数: | 91 次 | 
| 最近记录: |