在测试类的练习中,先编写了一个模块survey.py,模块中包含一个类AnonymousSurvey()
# -*- coding: utf-8 -*-
class AnonymousSurvey():
"""收集匿名调查问卷的答案"""
def __init__(self,question):
"""存储一个问题,并为存储答案做准备"""
self.question = question
self.responses = []
def show_question(self):
"""显示调查问卷"""
print(self.question)
def store_response(self,new_response):
"""存储单份调查问卷"""
self.responses.append(new_response)
def show_results(self):
"""显示收集的所有答案"""
print("Survey result:")
for response in responses:
print("- " + response)
测试类language_survey:
from survey import AnonymousSurvey
# 定义一个问题,并创建一个表示调查的AnonymousSurvey对象
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(question)
# 显示问题并存储答案
my_survey.show_question()
print("Enter 'q' at any time to quit.\n")
while True:
response = raw_input("Language: ")
if response == 'q':
break
my_survey.store_response(response)
# 显示调查结果
print("\nThank you to everyone who participated in the survey!")
my_survey.show_results()
结果一直报错:
D:\ProgramData\Anaconda2\python.exe F:/python/python_work/test/class/language_survey.py
What language did you first learn to speak?
Enter 'q' at any time to quit.
Language: Chinese
Language: English
Language: Spanish
Language: q
Thank you to everyone who participated in the survey!
Survey result:
Traceback (most recent call last):
File "F:/python/python_work/test/class/language_survey.py", line 19, in <module>
my_survey.show_results()
File "F:\python\python_work\test\class\survey.py", line 21, in show_results
for response in responses:
NameError: global name 'responses' is not defined
Process finished with exit code 1
运行结果显示报错,报错原因:global name 'responses' is not defined,意思是全局变量responses找不到,但我检查完代码,仍旧没发现错误,只好截图向大大请教。
原来都是我的粗心大意导致的,同时也反映了我对类的基本定义并没有完全理解,我赶紧翻书查阅在类中self的定义。
self:在每个与类相关联的方法调用都自动传递实参self,它是一个指向实例本身的引用,让实例能够访问类中的属性和方法
在survey.py模块第21行代码中因为丢失形参self,导致没法传递实参responses的内容,所以结果会报错变量responses找不到。
我按照大大的提示修改了代码,结果能正常运行了
survey.py
# -*- coding: utf-8 -*-
class AnonymousSurvey():
"""收集匿名调查问卷的答案"""
def __init__(self,question):
"""存储一个问题,并为存储答案做准备"""
self.question = question
self.responses = []
def show_question(self):
"""显示调查问卷"""
print(self.question)
def store_response(self,new_response):
"""存储单份调查问卷"""
self.responses.append(new_response)
def show_results(self):
"""显示收集的所有答案"""
print("Survey result:")
for response in self.responses:
print("- " + response)
测试类language_survey:
fromsurveyimportAnonymousSurvey
#定义一个问题,并创建一个表示调查的AnonymousSurvey对象
question="Whatlanguagedidyoufirstlearntospeak?"
my_survey=AnonymousSurvey(question)
#显示问题并存储答案
my_survey.show_question()
print("Enter'q'atanytimetoquit.\n")
whileTrue:
response=raw_input("Language:")
ifresponse=='q':
break
my_survey.store_response(response)
#显示调查结果
print("\nThankyoutoeveryonewhoparticipatedinthesurvey!")
my_survey.show_results()
运行结果:
What language did you first learn to speak?
Enter 'q' at any time to quit.
Language: Chinese
Language: English
Language: Spanish
Language: q
Thank you to everyone who participated in the survey!
Survey result:
- Chinese
- English
- Spanish
Process finished with exit code 0
网友评论