首页 > 其他分享 >实验7

实验7

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

实验任务1:

'''
银行账户
数据:持卡人姓名、账号、当前余额
操作:取款、存款、打印账户信息、返回账户余额
'''
class Account:
    '''一个模拟银行账户的简单类'''
    def __init__(self, name, account_number, initial_amount = 10):
        '''构造新账户'''
        self._name = name
        self._card_no = account_number
        self._balance = initial_amount
    def deposit(self, amount):
        '''存款'''
        self._balance += amount
    def withdraw(self, amount):
        '''取款'''
        if self._balance < amount:
            print('余额不足')
            return
        self._balance -= amount
    def info(self):
        '''打印账户信息'''
        print('持卡人姓名:', self._name)
        print('持卡人账号:', self._card_no)
        print('持卡人账户余额:', self._balance)
    def get_balance(self):
        '''返回账户余额'''
        return self._balance
def main():
    '''创建Account类对象,测试类'''
    print('测试账户1:'.center(30, '*'))
    a1 = Account('Bob', '5002311', 20000)
    a1.deposit(5000)
    a1.withdraw(4000)
    a1.info()
    print()
    print('测试账户2:'.center(30, '*'))
    a2 = Account('Joe', '5006692', 20000)
    a2.withdraw(10000)
    a2.withdraw(5000)
    a2.info()
if __name__ == '__main__':
    main()

 实验任务2:

'''
shape.py是一个图形类模块
包括:
基类: 图形类Shape
派生类: 矩形类Rect, 圆形类Circle, 三角形类Triangle
'''
 
class Shape:
    '''形状基类'''
    def info(self):
        '''打印图形信息'''
        pass
    def area(self):
        '''计算面积'''
        pass
    def perimeter(self):
        '''计算周长'''
        pass
 
class Rect(Shape):
    '''
    矩形类, 继承自Shape
    属性:矩形左上角点的坐标、宽、高
    方法: 打印矩形信息,计算面积、周长
    '''
    def __init__(self, x = 0, y = 0, length = 2, width = 1):
        '''构造矩形对象,根据矩形左上角顶点坐标(x,y)和长、宽'''
        self._x = x
        self._y = y
        self._width = width
        self._length = length
    def info(self):
        print(f'矩形左上角顶点坐标: ({self._x}, {self._y})')
        print(f'矩形长: {self._length}')
        print(f'矩形宽: {self._width}')
    def area(self):
        return self._length * self._width
    def perimeter(self):
        return (self._length + self._width) * 2
    
class Circle(Shape):
    '''
    圆形类,继承自Shape
    属性:圆心坐标、半径
    方法: 打印圆信息,计算面积、周长
    '''
    def __init__(self, x = 0, y = 0, radius = 1):
        '''构造圆形对象,圆心坐标(x,y), 半径r'''
        self._x = x
        self._y = y
        self._r = radius
    def info(self):
        print(f'圆心: ({self._x}, {self._y})')
        print(f'半径: {self._r}')
    def area(self):
        return 3.14 * self._r * self._r
    def perimeter(self):
        return 2 * 3.14 * self._r
class Triangle(Shape):
    '''
    三角形类,继承自Shape
    属性:三边边长
    方法:打印三角形信息,计算周长、面积
    '''
    def __init__(self, a = 1, b = 1, c = 1):
        self._a, self._b, self._c = a, b, c
    def info(self):
        print(f'三角形三边长: ({self._a}, {self._b}, {self._c})')
    def area(self):
        s = (self._a + self._b + self._c) / 2
        ans = (s*(s - self._a)*(s - self._b)*(s - self._c)) ** 0.5
        return ans
    def perimeter(self):
        return (self._a + self._b + self._c)
# 测试类
def main():
    print('测试1:'.center(40, '*'))
    shapes_lst1 = [Circle(), Rect(), Triangle()]
    for t in shapes_lst1:
        t.info()
        print(f'面积: {t.area():.2f}')
        print(f'周长: {t.perimeter():.2f}')
        print()
    print('测试2:'.center(40, '*'))
    shapes_lst2 = [Circle(x = 2, y = 2, radius = 10),
                    Rect(x = 50, y = 50, length = 10, width = 5),
                    Triangle(a = 3, b = 4, c = 5)]
    for t in shapes_lst2:
        t.info()
        print(f'面积: {t.area():.2f}')
        print(f'周长: {t.perimeter():.2f}')
        print()
if __name__ == '__main__':
    main()

 task2:

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

 实验任务3:

import math as ma
 
def func(x):
    m=0
    s=2
    t=(1/(ma.sqrt(2*ma.pi)*s))*ma.exp(-0.5*((x-m)/s)**2)
    return t
 
for i in range(1,10,2):
    print(f'x = {i}, f = {func(i):.8f}')

 实验任务4:

'''
模拟多次随机漫步
'''
 
from matplotlib import pyplot as plt
from random_walk import RandomWalk
from time import sleep
 
# 代码范例中,模拟两次随机漫步过程
n = 0
while n < 2:
    n += 1
 
    # 创建一个RandomWalk对象,调用其方法生成随机游走坐标
    rw = RandomWalk(50000)
    rw.fill_walk()
 
    # 用散点图进行可视化绘制
    plt.figure(figsize = (10, 6), dpi = 128)
    point_numbers = list(range(rw.num_points))
    plt.scatter(rw.x_values, rw.y_values, c=point_numbers,cmap=plt.cm.Blues, edgecolor='none', s=1)
    
    # 终点和起点
    plt.scatter(0, 0, c = 'grey', edgecolors='none', s=100)
    plt.scatter(rw.x_values[-1], rw.y_values[-1], c = 'red',edgecolors='none', s=100)
    
    # 隐藏坐标轴
    plt.axis('off')
    plt.show()    

 

 

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

相关文章

  • 实验七
    实验1task1.py实验源码:1classAccount:2'''一个模拟银行账户的简单类'''3def__init__(self,name,account_number,initial_amount=10):4'''构造新账户'''5self._name=name6......
  • 实验六
    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)......