首页 > 其他分享 >实验七

实验七

时间:2023-06-08 16:14:23浏览次数:25  
标签:__ info self 实验 ._ print def

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,account):
        self._balance+=account
    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():
    print('测试账户1:'.center(30,'*'))
    a1=Account('Bob','5002311',20000)
    a1.deposit(5000)
    a1.withdraw(4000)
    a1.info()

    print()

    print('测试账户2:'.center(30, '*'))
    a2 = Account('Joey', '5006692', 20000)
    a2.deposit(10000)
    a2.withdraw(5000)
    a2.info()
if __name__ == '__main__':
    main()

  

'''
shape.py是一个图形类模块
包括:
基类: 图形类Shape
派生类: 矩形类Rect, 圆形类Circle, 三角形类Triangle
'''

class Shape:
    def info(self):
        pass

    def area(self):
        pass
    def preimeter(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 2 * 3.14 * 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()

  

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 math import *
m = 0
s = 2
def func(x):
    return 1/(sqrt(2*pi)*s)*(exp((-0.5)*((x-m)/s)**2))


print(f'x = 1,f = {func(1):.8f}')
print(f'x = 3,f = {func(3):.8f}')
print(f'x = 5,f = {func(5):.8f}')
print(f'x = 7,f = {func(7):.8f}')
print(f'x = 9,f = {func(9):.8f}')

  

 

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

相关文章

  • 实验六
    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(......
  • 实验二 最速下降法程序设计
    一、实验目的通过最速下降法的程序设计,为今后的约束优化方法的学习和编程奠定基础;掌握负梯度方向的定义和最速下降法的迭代公式;通过此次实验,进一步巩固最速下降法的基本原理和思想。二、实验内容(1)求解无约束优化问题:(2)终止准则取;(3)完成最速下降法(负梯度法)的MATLAB编程、调试;(4)......