类方法调用报错,菜鸟请教-问答-阿里云开发者社区-阿里云

开发者社区> python小鸟> 正文

类方法调用报错,菜鸟请教

2018-11-14 17:01:04 1478 1

类定义:
class AnnoymousSurvey():

#define a class which collect answers of surveys

def __init__(self,question):
    self.question=question
    self.responses=[]

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

def store_question(self,new_response):
    self.responses.append(new_response)

def show_results(self):
    print("Survey results:")
    for response in responses:
        print(response)

类方法调用:
from survey import AnnoymousSurvey

question='which animal do you like ?'
mysurvey=AnnoymousSurvey(question)

mysurvey.show_question(question)

print('enter q to quit')

while True:

response=input('Animal:')
if response.lower()=='q':
    break
mysurvey.store_question(response)

mysurvey.show_results() --->这一步会报错:

Survey results:
Traceback (most recent call last):
File "language.py", line 16, in

mysurvey.show_results()    

File "E:python_codesurvey.py", line 16, in show_results

for response in responses:

NameError: name 'responses' is not defined

取消 提交回答
全部回答(1)
  • 健东
    2019-07-17 23:14:14

    同样是菜鸟,有没有会的老师

    0 0
+关注
0
文章
1
问答
推荐问答
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载