task1.py
code:
class Account: def __init__(self,name,account_number,initial_amount = 10): self._name = name self._card_no = account_number self._balance = initial_amount def deposit(self,amount): self._balance += amount def withdraw(self,amount): if self._balance < amount: print('余额不足') return self._balance -= amount def info(self): print('持卡人姓名:',self._name) print('持卡人账号:',self._card_no) print('持卡人账户余额:',self._balance) def main(): print('测试账户1:'.center(30,'*')) a1 = Account('Bob','5006692',20000) a1.deposit(5000) a1.withdraw(4000) a1.info() print() print('测试账户2:'.center(30,'*')) a2 = Account('Joe','5006692',20000) a2.withdraw(10000) a2.withdraw(5000) a2.info() if __name__ == '__main__': main()
output:
Understanding and Experiencing:
Account类共有 name、account_number和initial_amount这三个属性。其中我们所调用的是deposit和withdraw函数,其功能是在initial_amount中扣除或增加Balance。且程序还具有判断余额是否足够的功能。在编写代码def的时候没有注意main()的缩进导致无法调用main函数,因此在写代码时应要时刻注意缩进,利用tab键进行调试。
shape.py
code:
class Shape: def info(self): pass def area(self): pass def perimeter(self): pass class Rect(Shape): def __init__(self, x=0 , y=0 ,length = 2,width = 1): self._x = x self._y = y self._width = width self._length = length def info(self): print(f'矩形左上角顶点坐标:({self._x},{self._y})') print(f'矩形长:{self._length}') print(f'矩形宽:{self._width}') def area(self): return self._length * self._width def perimeter(self): return (self._width+self._length)*2 class Circle(Shape): def __init__(self, x=0, y=0, radius=1): self._x = x self._y = y self._r = radius def info(self): print(f'圆心: ({self._x}, {self._y})') print(f'半径: {self._r}') def area(self): return 3.14 * self._r * self._r def perimeter(self): return 2 * 3.14 * self._r class Triangle(Shape): def __init__(self, a=1, b=1, c=1): self._a, self._b, self._c = a, b, c def info(self): print(f'三角形三边长: ({self._a}, {self._b}, {self._c})') def area(self): s = (self._a + self._b + self._c) / 2 ans = (s * (s - self._a) * (s - self._b) * (s - self._c)) ** 0.5 return ans def perimeter(self): return (self._a + self._b + self._c) # 测试类 def main(): print('测试1:'.center(40, '*')) shapes_lst1 = [Circle(), Rect(), Triangle()] for t in shapes_lst1: t.info() print(f'面积: {t.area():.2f}') print(f'周长: {t.perimeter():.2f}') print() print('测试2:'.center(40, '*')) shapes_lst2 = [Circle(x=2, y=2, radius=10), Rect(x=50, y=50, length=10, width=5), Triangle(a=3, b=4, c=5)] for t in shapes_lst2: t.info() print(f'面积: {t.area():.2f}') print(f'周长: {t.perimeter():.2f}') print() if __name__ == '__main__': main()
output:
task2.py
code:
from shape import Rect, Circle shape_lst = [Rect(5, 5, 10, 5), Circle(), Circle(1, 1, 10)] for i in shape_lst: i.info() print(f'面积: {i.area(): .2f}') print(f'周长: {i.perimeter(): .2f}') print()
output:
Understanding and Experiencing:
在编写该代码时,学生遇到了以下报错:TypeError: Rect() takes no arguments,在不断的调整重复中程序始终报错,并随着修改报错类型也在不断变化。最终学生发现错误的来源为line9中
def __init__(self, x=0 , y=0 ,length = 2,width = 1):
被写成了:
def __int__(self, x=0 , y=0 ,length = 2,width = 1):
仅一个小小字符之差,给我带来了半个小时的困扰和迷惑!绝不是代码过于矫情,本身在pycharm的环境下纠错分类已经足够简单,更别提更加复杂的环境!事无巨细。
task3.py
code:
from math import * k = 1 for i in range(5): for (m, s, x) in [(0, 2, k)]: fx = (1 / (sqrt(2 * pi )*s)) * exp((-1 / 2) * ((x - m) / s) ** 2) print(f'x = {k}',',',f'f={"%.8f" %fx}') k += 2
output:
标签:__,info,内置,面向对象编程,self,模块,._,print,def From: https://www.cnblogs.com/Luke-FOX-31415926/p/17461904.html