首页 > 其他分享 >实验七

实验七

时间:2023-06-13 21:11:37浏览次数:21  
标签:__ info self 实验 ._ print def

实验任务一

task1

源代码:

'''
银行账户
数据:持卡人姓名、账号、当前余额
操作:取款、存款、打印账户信息、返回账户余额
'''

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

  运行结果截图

实验任务二

task1

源代码

class Shape:
    def info(self):
        pass

    def area(self):
        pass

    def perimeter(self):
        pass

class Rect(Shape):
    def __init__(self,x=0,y=0,length=2,width=1):
        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):
    def __init__(self,x=0,y=0,radius=1):
        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 3.14*2*self._r

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

  运行结果截图

实验任务三

源代码

from random import choice
class RandomWalk():
    def __init__(self,num_points=5000):
        self.num_points=num_points
        self.x_values=[0]
        self.y_values=[0]

    def fill_walk(self):
        while len(self.x_values)<self.num_points:
            x_direction=choice([1,-1])
            x_distance=choice([0,1,2,3,4])
            x_step=x_direction*x_distance

            y_direction=choice([1,-1])
            y_distance=choice([0,1,2,3,4])
            y_step=y_direction*y_distance

            if x_step==0 and y_step==0:
                continue

            next_x=self.x_values[-1]+x_step
            next_y=self.y_values[-1]+y_step

            self.x_values.append(next_x)
            self.y_values.append(next_y)

def main():
    rw=RandomWalk(5)
    rw.fill_walk()
    print(rw.x_values)
    print(rw.y_values)

if __name__=='__main__':
    main()

  运行结果截图

 

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

相关文章

  • 实验 7
    1classAccount:2def__init__(self,name,account_number,initial_amount=10):3self._name=name4self._card_no=account_number5self._balance=initial_amount6defdeposit(self,amount):7self._b......
  • 浪潮信息获颁泰尔实验室首张服务器碳足迹证书
    日前,权威检测机构中国泰尔实验室颁发首张服务器产品碳足迹认证证书。经过对原材料获取、整机生产、运输、包装、产品使用到产品拆解回收等全生命周期的评估测试,每万台浪潮信息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){......