首页 > 其他分享 >实验7

实验7

时间:2023-06-08 16:25:40浏览次数:35  
标签:__ 10 self 实验 ._ print def

#1

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

 #2

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

2.2

 1 #2.2
 2 from shape import Rect,Circle
 3 
 4 shape_lst=[Rect(5,5,10,5),Circle(),Circle(1,1,10)]
 5 
 6 for i in shape_lst:
 7     i.info()
 8     print(f'面积:{i.area():.2f}')
 9     print(f'周长:{i.perimeter():.2f}')
10     print()
View Code

#3

1 #3
2 from math import *
3 def func(x):
4     m=0
5     s=2
6     return (1/(2*pi)**0.5/s)*exp(-0.5*((x-m)/s)**2)
7 
8 for i in range(1,10,2):
9     print(f'x={i},y={func(i):.8f}')
View Code

#4

 1 #4.1
 2 from random import choice
 3 class RandomWalk():
 4     def __init__(self,num_points=5000):
 5         self.num_points=num_points
 6         self.x_values=[0]
 7         self.y_values=[0]
 8 
 9     def fill_walk(self):
10         while len(self.x_values)<self.num_points:
11             x_direction=choice([1,-1])
12             x_distance=choice([0,1,2,3,4])
13             x_step=x_direction*x_distance
14 
15             y_direction=choice([1,-1])
16             y_distance=choice([0,1,2,3,4])
17             y_step=y_direction*y_distance
18 
19             if x_step==0 and y_step==0:
20                 continue
21 
22             next_x=self.x_values[-1]+x_step
23             next_y=self.y_values[-1]+y_step
24 
25             self.x_values.append(next_x)
26             self.y_values.append(next_y)
27 
28 def main():
29     rw=RandomWalk(5)
30     rw.fill_walk()
31     print(rw.x_values)
32     print(rw.y_values)
33 
34 if __name__=='__main__':
35     main()
View Code

4.2

 1 #4.2
 2 from matplotlib import pyplot as plt
 3 from random_walk import RandomWalk
 4 from time import sleep
 5 
 6 n=0
 7 while n<2:
 8     n+=1
 9     rw=RandomWalk(50000)
10     rw.fill_walk()
11 
12     plt.figure(figsize=(10,6),dpi=128)
13     point_numbers=list(range(rw.num_points))
14     plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolors='none',s=1)
15 
16     plt.scatter(0,0,c='grey',edgecolors='none',s=100)
17     plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100)
18 
19     plt.axis('off')
20     plt.show()
View Code

 

标签:__,10,self,实验,._,print,def
From: https://www.cnblogs.com/047lishiqi/p/17462922.html

相关文章

  • 实验七
    classAccount:def__init__(self,name,account_number,initial_amount=10):self._name=nameself._card_no=account_numberself._balance=initial_amountdefdeposit(self,account):self._balance+=accountdefwithdraw(s......
  • 实验六
    task11fromturtleimport*2defmove(x,y):3penup()4goto(x,y)5pendown()6defdraw(n,size=100):7foriinrange(n):8fd(size)9left(360/n)10defmain():11pensize(2)12pencolor('red......
  • 实验七
    任务一1classAccount:2'''一个模拟银行账户的简单类'''3def__init__(self,name,account_number,initial_amount=10):4'''构造新账户'''5self._name=name6self._card_no......
  • 《大学物理实验上》期末笔记(三)作图法以及一些实验
    《大学物理实验上》期末笔记(三)作图法以及一些实验数据处理有多种方法,下面仅就作图法、逐差法作简单介绍。作图法就考试来说,结果不是最主要的,过程才重要。评分标准(共15分):图的题目——1分横坐标的物理符号与单位、还有分度选择——各1分,共3分纵坐标的物理符号与单位、还有分......
  • 实验7 面向对象编程与内置模块
    实验任务1task1.py实验源码:classAccount:'''一个模拟银行账户的简单类'''def__init__(self,name,account_number,initial_amount=10):'''构造新账户'''self._name=nameself._card_no......
  • 实验6
    实验任务1task1fromturtleimport*defmove(x,y):penup()goto(x,y)pendown()defdraw(n,size=100):foriinrange(n):fd(size)left(360/n)defmain():pensize(2)pencolor('red')move(-200,0)......
  • 实验6
    实验6task1_1.pyfromturtleimport*defmove(x,y):'''画笔移动到坐标(x,y)处'''penup()goto(x,y)pendown()defdraw(n,size=100):'''绘制边长为size的正n边形'''foriinrange(n):......
  • SDN实验指导书目录
    Mininet入门与实战1.1Mininet源码安装与验证1.2Mininet拓扑构建与命令使用1.3Mininet调用API扩展自定义拓扑1.4Mininet可视化构建网络拓扑1.5Mininet流表应用实战1——手动添加流表1.6Mininet流表应用实战2——控制器下发流表协议分析1.7Mininet多数据中心网络拓扑流量......
  • 1.7Mininet多数据中心网络拓扑流量带宽实验
    Mininet多数据中心网络拓扑流量带宽实验实验目的1、掌握多数据中心网络拓扑的构建。2、熟悉网络性能测试工具Iperf,根据实验测试SDN网络的性能。实验环境Mininet多数据中心网络拓扑流量带宽实验的拓扑如下图所示。设备名称软件环境硬件环境主机Ubuntu14.04桌面......
  • 实验6
    5:1fromturtleimport*23defmove(x,y):4penup()5goto(x,y)6pendown()789move(-100,0)10begin_fill()11foriinrange(4):12fd(200)13left(90)14end_fill()1516move(0,0)17begin_fill()18pencolor(......