task1
实验源码:
1 #1 2 class Account: 3 def __init__(self,name,account_number,initial_amount=10): 4 self._name=name 5 self._card_no=account_number 6 self._balance=initial_amount 7 8 def deposit(self,amount): 9 '''存款''' 10 self._balance+=amount 11 12 def withdraw(self,amount): 13 '''取款''' 14 if self._balance<amount: 15 print('余额不足') 16 return 17 self._balance-=amount 18 19 def info(self): 20 '''打印账户信息''' 21 print('持卡人姓名:',self._name) 22 print('持卡人账号:',self._card_no) 23 print('持卡人账户余额:',self._balance) 24 25 def get_balance(self): 26 '''返回账户余额''' 27 return self._balance 28 29 def main(): 30 print('测试账户1:'.center(30,'*')) 31 a1=Account('Bob','5002311',20000) 32 a1.deposit(5000) 33 a1.withdraw(4000) 34 a1.info() 35 36 print() 37 38 print('测试账户2:'.center(30,'*')) 39 a1=Account('Joe','5006692',20000) 40 a1.deposit(10000) 41 a1.withdraw(5000) 42 a1.info() 43 44 if __name__=='__main__': 45 main()
实验结果:
task2
实验源码:
#2 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._length+self._width)*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 3.14*2*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()
实验结果:
task3
实验源码:
#3 from math import * def func(x): m=0 s=2 return (1/(2*pi)**0.5/s)*exp(-0.5*((x-m)/s)**2) for i in range(1,10,2): print(f'x={i},y={func(i):.8f}')
实验结果:
task4
实验源码:
#4.1 from random import choice class RandomWalk(): def __init__(self,num_points=5000): self.num_points=num_points self.x_values=[0] self.y_values=[0] def fill_walk(self): while len(self.x_values)<self.num_points: x_direction=choice([1,-1]) x_distance=choice([0,1,2,3,4]) x_step=x_direction*x_distance y_direction=choice([1,-1]) y_distance=choice([0,1,2,3,4]) y_step=y_direction*y_distance if x_step==0 and y_step==0: continue next_x=self.x_values[-1]+x_step next_y=self.y_values[-1]+y_step self.x_values.append(next_x) self.y_values.append(next_y) def main(): rw=RandomWalk(5) rw.fill_walk() print(rw.x_values) print(rw.y_values) if __name__=='__main__': main()
实验结果:
标签:__,内置,return,面向对象编程,self,模块,._,print,def From: https://www.cnblogs.com/scy2003/p/17476160.html