视频总结
final exam :
报错:
'int' object is not callable,此参数已被声明为int,不可作为函数调用
stackoverflow:https://stackoverflow.com/questions/9767391/typeerror-int-object-is-not-callable
原因:调用函数时传参了int类型,实际上应该参数定义是函数,之后的函数调用就会报此错误
python中的self
-
__init__
函数默认的第一个参数 - 类中方法的第一参数永远是类的本身实例变量self,并且调用时,不用传递
第二题答案
# Paste your entire definition of the edx class in this box
class edx(object):
# edc类
def __init__(self, courses):
# 初始化 我的课程
# 用一个数组初始化
self.myCourses = []
for course in courses:
self.myCourses.append(courseInfo(course))
def setGrade(self, grade, course="6.01x"):
"""
grade: integer greater than or equal to 0 and less than or equal to 100
course: string
This method sets the grade in the courseInfo object named by `course`.
If `course` was not part of the initialization, then no grade is set, and no
error is thrown.
The method does not return a value.
"""
# fill in code to set the grade
for index in range(len(self.myCourses)):
if(self.myCourses[index].courseName == course):
self.myCourses[index].setGrade(grade)
def getGrade(self, course="6.02x"):
"""
course: string
This method gets the grade in the the courseInfo object named by `course`.
returns: the integer grade for `course`.
If `course` was not part of the initialization, returns -1.
"""
# fill in code to get the grade
for index in range(len(self.myCourses)):
if(self.myCourses[index].courseName == course):
return self.myCourses[index].getGrade()
def setPset(self, pset, score, course="6.00x"):
"""
pset: a string or a number
score: an integer between 0 and 100
course: string
The `score` of the specified `pset` is set for the
given `course` using the courseInfo object.
If `course` is not part of the initialization, then no pset score is set,
and no error is thrown.
"""
# fill in code to set the pset
for index in range(len(self.myCourses)):
if(self.myCourses[index].courseName == course):
self.myCourses[index].setPset(pset,score)
def getPset(self, pset, course="6.00x"):
"""
pset: a string or a number
course: string
returns: The score of the specified `pset` of the given
`course` using the courseInfo object.
If `course` was not part of the initialization, returns -1.
"""
# fill in code to get the pset
for index in range(len(self.myCourses)):
if(self.myCourses[index].courseName == course):
self.myCourses[index].getPset(pset)
chp 11
PyLab类似matlab绘图,练习绘图,完整代码如下:
import pylab
class Mortgage(object):
"""建立不同种类抵押贷款的抽象类"""
def __init__(self, loan, annRate, months):
self.loan = loan
self.rate = annRate/12.0
self.months = months
self.paid = [0.0]
self.outstanding = [loan]
self.payment = findPayment(loan, self.rate, months)
self.legend = None #description of mortgage
def makePayment(self):
self.paid.append(self.payment)
reduction = self.payment - self.outstanding[-1]*self.rate
self.outstanding.append(self.outstanding[-1] - reduction)
def getTotalPaid(self):
return sum(self.paid)
def __str__(self):
return self.legend
def plotPayments(self, style):
pylab.plot(self.paid[1:], style, label = self.legend)
def plotBalance(self, style):
pylab.plot(self.outstanding, style, label = self.legend)
def plotTotPd(self, style):
totPd = [self.paid[0]]
for i in range(1, len(self.paid)):
totPd.append(totPd[-1] + self.paid[i])
pylab.plot(totPd, style, label = self.legend)
def plotNet(self, style):
totPd = [self.paid[0]]
for i in range(1, len(self.paid)):
totPd.append(totPd[-1] + self.paid[i])
equityAcquired = pylab.array([self.loan] * len(self.outstanding))
equityAcquired = equityAcquired - pylab.array(self.outstanding)
net = pylab.array(totPd) – equityAcquired
pylab.plot(net, style, label = self.legend)
网友评论