task1
1 class Account: 2 3 def __init__(self, name, account_number, initial_amount = 10): 4 self._name = name 5 self._card_no = account_number 6 self._blance = initial_amount 7 8 def deposit(self, amount): 9 self._blance += amount 10 11 def withdraw(self, amount): 12 if self._blance < amount: 13 print('余额不足') 14 return 15 16 self._blance -= amount 17 18 def info(self): 19 print('持卡人姓名:', self._name) 20 print('持卡人账户:', self._card_no) 21 print('持卡人账户余额:', self._blance) 22 23 def get_blance(self): 24 return self._blance 25 26 def main(): 27 print('测试账户1:'.center(30, '*')) 28 a1 = Account('Bob', '500669', 20000) 29 a1.deposit(500) 30 a1.withdraw(400) 31 a1.info() 32 33 print() 34 35 print('测试账户2'.center(30, '*')) 36 a2 = Account('Alice', '500668', 30000) 37 a2.withdraw(10000) 38 a2.withdraw(30000) 39 a2.info() 40 41 if __name__ == '__main__': #后面的内容只有在自己使用的时候才会被引用,如果其他的程序不会运行 42 main()
结果:
task2
1 from shape_mood import Rect, Circle 2 3 shape_lst = [Rect(5, 5, 10, 5), Circle(), Circle(1, 1, 10)] 4 5 for i in shape_lst: 6 i.info() 7 print(f'面积:{i.area():.2f}') 8 print(f'周长:{i.perimeter():.2f}') 9 print()
结果:
task3
1 def func(x, m = 0, s = 2): 2 import math 3 x1 = 1/(math.sqrt(2 * math.pi) * s) 4 x2 = (-1/2) * ((x-m)/s)**2 5 x3 = math.exp(x2) 6 7 return x1 * x3 8 9 def main(): 10 fun_lst = [1, 3, 5, 7, 9] 11 for i in fun_lst: 12 print(f'x = {i}', end = ',') 13 print(f'f = {func(i):.8f}') 14 15 if __name__ == '__main__': 16 main()
结果:
标签:__,内置,面向对象编程,self,amount,模块,._,print,def From: https://www.cnblogs.com/ningqiningba/p/17478982.html