首页 > 其他分享 >实验7

实验7

时间:2023-06-12 22:34:48浏览次数:30  
标签:__ return self 实验 ._ print def

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

运行结果:

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}')

运行结果:

task4.py

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

运行结果:

 

标签:__,return,self,实验,._,print,def
From: https://www.cnblogs.com/hyy052416/p/17476274.html

相关文章

  • 实验7
    task1_1#include<stdio.h>#defineN7#defineM80typedefstruct{charname[M];charauthor[M];}Book;intmain(){Bookx[N]={{"《雕塑家》","斯科特·麦克劳德"},{"《灯塔》","克里斯多夫·夏布特"},......
  • 实验7
    实验任务1实验源码1classAccount:2def__init__(self,name,account_number,initial_amount=10):3self._name=name4self._card_no=account_number5self._balance=initial_amount6defdeposit(self,amount):7......
  • 实验7 面向对象编程与内置模块
    task1程序源码:1'''2银行账户3数据:持卡人姓名、账号、当前余额4操作:取款、存款、打印账户信息、返回账户余额5'''6classAccount:7'''一个模拟银行账户的简单类'''89def__init__(self,name,account_number,initial_amount=1......
  • 实验7 面向对象编程与内置模块
    task1实验源码:1#12classAccount:3def__init__(self,name,account_number,initial_amount=10):4self._name=name5self._card_no=account_number6self._balance=initial_amount78defdeposit(self,amount):9......
  • 实验7 面向对象编程与内置模块
    实验任务1task1.py程序源码:1'''2银行账户3数据:持卡人姓名、账号、当前余额4操作:取款、存款、打印账户信息、返回账户余额5'''67classAccount:8'''一个模拟银行账户的简单类'''9def__init__(self,name,account_number,initial_a......
  • 实验7
    task1.py实验源码:1classAccount:2#一个模拟账户类3def__init__(self,name,account_number,initial_amount=10):4'''构造新账户'''5self._name=name6self._card_no=account_number7sel......
  • CJJC项目实验环境配置
    CJJC项目实验环境配置 (base)D:\>(base)D:\>condacreate-nwind_2022python==3.7Solvingenvironment:done##PackagePlan##environmentlocation:D:\Anaconda3\envs\wind_2022added/updatedspecs:-python==3.7Thefollowingpackages......
  • 实验7
    实验任务1task1源代码:classAccount:'''一个模拟银行账户的简单类'''def__init__(self,name,account_number,initial_amount=10):'''构造新账户'''self._name=nameself._card_no=ac......
  • 实验7
    实验任务1:'''银行账户数据:持卡人姓名、账号、当前余额操作:取款、存款、打印账户信息、返回账户余额'''classAccount:'''一个模拟银行账户的简单类'''def__init__(self,name,account_number,initial_amount=10):'''构造新......
  • 实验七
    实验1task1.py实验源码:1classAccount:2'''一个模拟银行账户的简单类'''3def__init__(self,name,account_number,initial_amount=10):4'''构造新账户'''5self._name=name6......