首页 > 其他分享 >实验七

实验七

时间:2023-06-12 17:24:19浏览次数:36  
标签:__ info self 实验 ._ print def

实验1

task1.py

实验源码:

 1 class Account:
 2     '''一个模拟银行账户的简单类'''
 3     def __init__(self, name, account_number, initial_amount = 10):
 4         '''构造新账户'''
 5         self._name = name
 6         self._card_no = account_number
 7         self._balance = initial_amount
 8     def deposit(self, amount):
 9         '''存款'''
10         self._balance += amount
11     def withdraw(self, amount):
12         '''取款'''
13         if self._balance < amount:
14             print('余额不足')
15             return
16 
17         self._balance -= amount
18 
19     def info(self):
20 
21 
22         print('持卡人姓名:', self._name)
23         print('持卡人账号:', self._card_no)
24         print('持卡人账户余额:', self._balance)
25 
26     def get_balance(self):
27         return self._balance
28 
29 def main():
30     '''创建Account类对象,测试类'''
31 
32     print('测试账户1:'.center(30, '*'))
33     a1 = Account('Bob', '5002311', 20000)
34     a1.deposit(5000)
35     a1.withdraw(4000)
36     a1.info()
37 
38     print()
39 
40     print('测试账户2:'.center(30, '*'))
41     a2 = Account('Joe', '5006692', 20000)
42     a2.withdraw(10000)
43     a2.withdraw(5000)
44     a2.info()
45 if __name__ == '__main__':
46     main()

实验结果:

 task2.py

实验源码:

  1 '''
  2 shape.py是一个图形类模块
  3 包括:
  4 基类: 图形类Shape
  5 派生类: 矩形类Rect, 圆形类Circle, 三角形类Triangle
  6 '''
  7 
  8 class Shape:
  9     '''形状基类'''
 10     def info(self):
 11         '''打印图形信息'''
 12         pass
 13 
 14 def area(self):
 15     '''计算面积'''
 16     pass
 17 
 18 def perimeter(self):
 19     '''计算周长'''
 20     pass
 21 
 22 class Rect(Shape):
 23     '''
 24     矩形类 , 继承自Shape
 25     属性:矩形左上角点的坐标、宽、高
 26     方法:  打印矩形信息,计算面积、周长
 27     '''
 28     def __init__(self, x = 0, y = 0, length = 2, width = 1):
 29         '''构造矩形对象,根据矩形左上角顶点坐标(x,y)和长、宽 '''
 30 
 31         self._x = x
 32         self._y = y
 33         self._width = width
 34         self._length = length
 35 
 36     def info(self):
 37         print(f'矩形左上角顶点坐标: ({self._x}, {self._y})')
 38         print(f'矩形长 : {self._length}')
 39         print(f'矩形宽 : {self._width}')
 40 
 41     def area(self):
 42         return self._length * self._width
 43 
 44     def perimeter(self):
 45         return (self._length + self._width) * 2
 46 
 47 class Circle(Shape):
 48 
 49 
 50     def __init__(self, x=0, y=0, radius=1):
 51         self._x = x
 52         self._y = y
 53         self._r = radius
 54 
 55     def info(self):
 56         print(f'圆心 : ({self._x}, {self._y})')
 57         print(f'半径 : {self._r}')
 58 
 59     def area(self):
 60         return 3.14 * self._r * self._r
 61 
 62     def perimeter(self):
 63         return 2 * 3.14 * self._r
 64 
 65 class Triangle(Shape):
 66     def __init__(self, a=1, b=1, c=1):
 67         self._a, self._b, self._c = a, b, c
 68 
 69     def info(self):
 70         print(f'三角形三边长 : ({self._a}, {self._b}, {self._c})')
 71 
 72     def area(self):
 73         s = (self._a + self._b + self._c) / 2
 74         ans = (s * (s - self._a) * (s - self._b) * (s - self._c)) ** 0.5
 75 
 76         return ans
 77 
 78     def perimeter(self):
 79         return (self._a + self._b + self._c)
 80 
 81 # 测试类
 82 def main():
 83     print('测试1: '.center(40, '*'))
 84 
 85     shapes_lst1 = [Circle(), Rect(), Triangle()]
 86 
 87     for t in shapes_lst1:
 88         t.info()
 89         print(f'面积 : {t.area():.2f}')
 90         print(f'周长 : {t.perimeter():.2f}')
 91         print()
 92 
 93     print('测试2: '.center(40, '*'))
 94 
 95     shapes_lst2 = [Circle(x=2, y=2, radius=10),
 96                    Rect(x=50, y=50, length=10, width=5), Triangle(a=3, b=4, c=5)]
 97     for t in shapes_lst2:
 98         t.info()
 99         print(f'面积 : {t.area():.2f}')
100         print(f'周长 : {t.perimeter():.2f}')
101         print()
102 
103 if __name__ == '__main__':
104     main()
1 from Shape 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.py

实验源码:

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

实验结果:

 

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

相关文章

  • 实验六
    task11fromturtleimport*2defmove(x,y):34penup()5goto(x,y)6pendown()7defdraw(n,size=100):89foriinrange(n):10fd(size)11left(360/n)12defmain():13pensize(2)14pencolor(&......
  • 实验六
    task1-1fromturtleimport*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)d......
  • 实验6
    实验任务1task1_1.py1fromturtleimport*2defmove(x,y):3'''画笔移动到坐标(x,y)处'''4penup()5goto(x,y)6pendown()78defdraw(n,size=100):9'''绘制边长为size的正n变形''&#......
  • 实验6 turtle绘图与python库应用编程体验
    实验任务1fromturtleimport*defmove(x,y):'''画笔移动到坐标(x,y)处'''penup()goto(x,y)pendown()defdraw(n,size=100):'''绘制边长为size的正n边形'''foriinrange(n):fd(siz......
  • Linux操作系统实训操作五(实验效果)
    Linux操作系统实训操作五(实验效果)原创 宇翔2020 网路小栈 2023-04-1007:00 发表于山东收录于合集#Linux操作系统2个#CentOS4个 1、新建用户jyzy(计应专业)、dmzy(动漫专业)、shzy(书画专业)、hkzy(航空专业),用tail命令查看/etc/passwd文件的后四行内容。(截图)。 ......
  • 实验六
    实验任务1task1.1实验源码1fromturtleimport*2defmove(x,y):3penup()4goto(x,y)5pendown()6defdraw(n,size=100):7foriinrange(n):8fd(size)9left(360/n)10defmain():11pensize(2)12p......
  • php文件上传之白名单00截断实验
    %00截断介绍:0x00,%00,/00在url中%00表示ascll码中的0,而ascii中0作为特殊字符保留,表示字符串结束,所以当url中出现%00时就会认为读取已结束。但是所谓的if拦截仍会读取后面的后缀达到绕过白名单的效果。当前版本环境:PHP版本低于5.4.24,或者PHP版本在5.5.8到5.6.0之间,且GPC......
  • 实验六
    实验任务一task1_1实验源码:1fromturtleimport*23defmove(x,y):4penup()5goto(x,y)6pendown()7defdraw(n,size=100):#绘制边长为size的正n边形8foriinrange(n):9fd(size)10left(360/n)11defmain():12......
  • Python实验课6
    实验任务一实验源码:fromturtleimport*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)......
  • 实验六
    实验1task1.1实验源码1fromturtleimport*23defmove(x,y):4penup()5goto(x,y)6pendown()78defdraw(n,size=100):9foriinrange(n):10fd(size)11left(360/n)1213defmain():14pensize(2)15......