今天也要开心咯 2021-12-22 15:37 采纳率: 100%
浏览 40
已结题

TypeError: argument of type 'AnonymousSurvey' is not iterable

测试代码时一直显示题目中的报错,但是定义的功能函数又可以正常运行,不太清楚是哪里出了错
定义的类的代码

class AnonymousSurvey():
'''匿名调查问卷'''
def init(self,question):
self.question=question
self.responses=[]

def show_question(self):
    print(self.question)

def store_response(self,response):
    self.responses.append(response)


def show_results(self):
    print('The survey results:')
    #for response in self.responses:
    print(self.responses)

测试函数代码
import unittest
from survey import AnonymousSurvey
'''测试匿名调查的正确性'''
class LanguageTestCase(unittest.TestCase):

def setUp(self):
    question='你常用的语言?'
    self.my_survey= AnonymousSurvey(question)
    self.response='python'

def test_one_language_survey(self):
    self.my_survey.store_response(self.response)
    self.assertIn(self.response,self.my_survey)

unittest.main()

运行结果及报错内容

E

ERROR: test_one_language_survey (main.LanguageTestCase)

Traceback (most recent call last):
File "D:/Programs/pycharm/PyCharm 2019.3.3/untitled3/venv/Scripts/languagetest.py", line 13, in test_one_language_survey
self.assertIn(self.response,self.my_survey)
File "D:\Programs\Python\Python38\lib\unittest\case.py", line 1176, in assertIn
if member not in container:
TypeError: argument of type 'AnonymousSurvey' is not iterable

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

2条回答 默认 最新

  • 於黾 2021-12-22 15:44
    关注

    AnonymousSurvey是个类,不是集合,不可迭代
    你把它这样传进函数里self.assertIn(self.response,self.my_survey)
    然后又这样用
    if member not in container:
    你这是默认它是个迭代器了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月30日
  • 已采纳回答 12月22日
  • 创建了问题 12月22日

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?