我正在尝试构建一个与松弛连接的机器人,并不断收到TypeError
:
Traceback (most recent call last):
File "happybotV4.2.py", line 217, in <module>
av_mood, user_count, total_mood = datamood(user_answer, user_id, user_count, total_mood, av_mood)
TypeError: 'NoneType' object is not iterable
代码如下:
if slackinput_list and len(slackinput_list) > 0:
user_answer, user_id, channel, event_type = parse_slack_useranswer(slackinput_list)
#print(user_answer,user_id)#test
if user_answer and user_id and user_id != 'U7GRT34H3' and event_type=="message":
if user_answer=="1" or user_answer=="5":
print(user_answer, user_id, user_count, total_mood, av_mood) #test
#if user_id not in user_list:
av_mood, user_count, total_mood = datamood(user_answer, user_id, user_count, total_mood, av_mood)
以及datamood的代码:
def datamood(mood, user, count, total_mood, average_mood):
print(user_list)
if user not in user_list:
count = count+1
mood_user.append(mood)
user_list.append(user)
print(user_list, user)
if len(mood_user)>0:
for i in mood_user :
if i=="1":
n=1
elif i=="5":
n=5
total_mood = total_mood + n
print(total_mood)#test
average_mood = total_mood/count
print (average_mood, count) #test
return average_mood, count, total_mood
else:
return average_mood, count, total_mood
事实上datamood只能工作一次,但是当我第二次尝试调用它时,会收到错误消息。我检查了每个变量(即user_answer
,user_id
,user_count
,total_mood
,av_mood
)是否均为None且格式正确。>
有什么想法吗?
答案 0 :(得分:1)
您对datamood()
方法的调用期望返回一个值。由于当user
中存在user_list
时,您的逻辑没有明确告诉您的方法,该方法将返回None
或NoneType
对象。如果未在Python方法中显式设置返回值,它将始终返回None
。因此,当您第一次调用该方法而user
不存在时,则需要返回值。但是,当您调用该方法并且user
确实存在时,您的方法将返回None
。