使用PYTHON Wit.ai如果traits:TypeError中没有trait,则不能迭代类型为'NoneType'的参数

问题描述

def first_trait_value(traits,trait):
  """
  Returns first trait value
  """
  if trait not in traits:
    return None
  val = traits[trait][0]['value']
  if not val:
    return None
return val

从Facebook页面输入消息时出现错误。但是当我把信息手册放进去的时候。没有错误发生。上面是那个功能

def message_form_entity(msg):
  response = None
  entity,trait,value = wit_response(msg)
  greeting = first_trait_value(trait,'wit$greetings')
  sentiment = first_trait_value(trait,'wit$sentiment')
  course = first_trait_value(trait,'wit_course')
  print(sentiment,greeting,course)
  if greeting:
    response = "{}! \nwelcome suza bot.\ncan I help you ?".format(str(value))
  elif entity == 'closes:closes':
    response = "{} :) \n you are welcome".format(str(value))
  elif course:
    response = "PhD in Kiswahili. \n Masters of Chemistry. \nMasters of  Science in Environmental Science. \nDegree of Science in Environmental Health. \nBachelor Degree of information Application and Management. \nBachelor Degree of Arts in Geography and Environmental Studies."
#     # response = random.choice(resp) 
  elif sentiment:
    response = 'Glad you liked it.' if sentiment == 'positive' else 'Hmm.' 
  if response == None:
    response = 'sorry not understand this'

  return (response)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)