首页 > 其他分享 >实验 7

实验 7

时间:2023-06-13 20:23:09浏览次数:22  
标签:__ info self 实验 ._ print def

 
1 class Account: 2 def __init__(self, name, account_number, initial_amount = 10): 3 self._name = name 4 self._card_no = account_number 5 self._balance = initial_amount 6 def deposit(self, amount): 7 self._balance += amount 8 def withdraw(self, amount): 9 if self._balance < amount: 10 print('余额不足') 11 return 12 13 self._balance -= amount 14 15 def info(self): 16 print('持卡人姓名:', self._name) 17 print('持卡人账号:', self._card_no) 18 print('持卡人账户余额:', self._balance) 19 def get_balance(self): 20 return self._balance 21 def main(): 22 print('测试账户1:'.center(30, '*')) 23 a1 = Account('Bob', '5002311', 20000) 24 a1.deposit(5000) 25 a1.withdraw(4000) 26 a1.info() 27 print() 28 print('测试账户2:'.center(30, '*')) 29 a2 = Account('Joe', '5006692', 20000) 30 a2.withdraw(10000) 31 a2.withdraw(5000) 32 a2.info() 33 if __name__ == '__main__': 34 main()

 

task 2

 1 class Shape:
 2     def info(self):
 3         pass
 4     def area(self):
 5         pass
 6     def preimeter(self):
 7         pass
 8 class Rect(Shape):
 9     def __init__(self, x = 0, y = 0, length = 2, width = 1):
10         self._x = x
11         self._y = y
12         self._width = width
13         self._length = length
14     def info(self):
15         print(f'矩形左上角顶点坐标: ({self._x}, {self._y})')
16         print(f'矩形长: {self._length}')
17         print(f'矩形宽: {self._width}')
18     def area(self):
19         return self._length * self._width
20     def perimeter(self):
21         return (self._length + self._width) * 2
22 class Circle(Shape):
23     def __init__(self, x = 0, y = 0, radius = 1):
24         self._x = x
25         self._y = y
26         self._r = radius
27     def info(self):
28         print(f'圆心: ({self._x}, {self._y})')
29         print(f'半径: {self._r}')
30     def area(self):
31         return 3.14 * self._r * self._r
32     def perimeter(self):
33         return 2 * 3.14 * self._r
34 class Triangle(Shape):
35     def __init__(self, a = 1, b = 1, c = 1):
36         self._a, self._b, self._c = a, b, c
37     def info(self):
38         print(f'三角形三边长: ({self._a}, {self._b}, {self._c})')
39     def area(self):
40         s = (self._a + self._b + self._c) / 2
41         ans = (s*(s - self._a)*(s - self._b)*(s - self._c)) ** 0.5
42         return ans
43     def perimeter(self):
44         return (self._a + self._b + self._c)
45 def main():
46     print('测试1:'.center(40, '*'))
47     shapes_lst1 = [Circle(), Rect(), Triangle()]
48     for t in shapes_lst1:
49         t.info()
50         print(f'面积: {t.area():.2f}')
51         print(f'周长: {t.perimeter():.2f}')
52         print()
53     print('测试2:'.center(40, '*'))
54     shapes_lst2 = [Circle(x = 2, y = 2, radius = 10),
55                    Rect(x = 50, y = 50, length = 10, width = 5),
56                    Triangle(a = 3, b = 4, c = 5)]
57     for t in shapes_lst2:
58         t.info()
59         print(f'面积: {t.area():.2f}')
60         print(f'周长: {t.perimeter():.2f}')
61         print()
62 if __name__ == '__main__':
63     main()

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()

from math import *
m = 0
s = 2
def func(x):
    return 1/(sqrt(2*pi)*s)*(exp((-0.5)*((x-m)/s)**2))
print(f'x = 1,f = {func(1):.8f}')
print(f'x = 3,f = {func(3):.8f}')
print(f'x = 5,f = {func(5):.8f}')
print(f'x = 7,f = {func(7):.8f}')
print(f'x = 9,f = {func(9):.8f}')

 

标签:__,info,self,实验,._,print,def
From: https://www.cnblogs.com/songsongsong/p/17478657.html

相关文章

  • 浪潮信息获颁泰尔实验室首张服务器碳足迹证书
    日前,权威检测机构中国泰尔实验室颁发首张服务器产品碳足迹认证证书。经过对原材料获取、整机生产、运输、包装、产品使用到产品拆解回收等全生命周期的评估测试,每万台浪潮信息NF5280M6全生命周期较业界平均水平可节约碳排放686吨,相当于植树5万棵,达到业内领先水平。*泰尔实验室作为......
  • 实验七 面向对象编程与内置模块
    实验任务1task1.pyclassAccount:def__init__(self,name,account_number,initial_amount=10):self._name=nameself._card_no=account_numberself._balance=initial_amountdefdeposit(self,amount):self._balance+=amoun......
  • 实验7
    TEST4#include<stdio.h>#include<stdlib.h>intmain(){intnum=0;charch;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){printf("fail\n");return1;}while(!feof(fp))......
  • 实验七
    classAccount:def__init__(self,name,account_number,initial_amount=10):self._name=nameself._card_no=account_numberself._balance=initial_amountdefdeposit(self,amount):self._balance+=amountdefwithd......
  • c语言:计数器实验
    要求:P1口接2只LED灯,定时器T1采用计数模式,方式1中断,外接按钮开关作为计数输入,当按2次按钮开关,P1口第一只LED点亮,再按2次按钮开关,P1口第二只LED点亮,再按2次按钮,所有LED灯熄灭。 1#include<reg52.h>23//定义LED灯的控制端口和对应的控制位4sbitLED1=P1^0;5......
  • 实验7
    任务4程序源码:#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>intmain(){charch;FILE*fp;inti=0;fp=fopen("data4.txt","r");ch=fgetc(fp);while(ch!=EOF){if(ch!=''&......
  • 实验七
    task11classAccount:2def__init__(self,name,account_number,initial_amount=10):3self._name=name4self._card_no=account_number5self._balance=initial_amount67defdeposit(self,amount):8self.......
  • 实验7
    task3importmathdeffunc(x):f=((math.e)**((-0.5)*(((x-m)/s)**2)))/(s*math.sqrt(2*(math.pi)))returnf'{f:.8f}'m=0s=2x_lst=[1,3,5,7,9]forxinx_lst:print(f'x={x},f={func(x)}') ......
  • 实验七
    task4代码#include<stdio.h>#include<stdlib.h>#include<string.h>#defineN5#defineM80intmain(){charch[M];intch_counts=0,n=0;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){......
  • Python实验课7
    实验任务1classAccount:'''一个模拟银行账户的简单类'''def__init__(self,name,account_number,initial_amount=10):'''构造新账户'''self._name=nameself._card_no=account_numb......