首页 > 其他分享 >实验七

实验七

时间:2023-06-13 14:35:14浏览次数:29  
标签:__ 10 self 实验 ._ print def

task1

 1 class Account:
 2     def __init__(self, name, account_number, initial_amount = 10):
 3         self._name=name
 4         self._card_no=account_number
 5         self._balance=initial_amount
 6 
 7     def deposit(self,amount):
 8         self._balance+=amount
 9 
10     def withdraw(self,amount):
11         if self._balance<amount:
12             print('余额不足')
13             return
14 
15         self._balance-=amount
16 
17     def info(self):
18         print('持卡人姓名:',self._name)
19         print('持卡人账号:',self._card_no)
20         print('持卡人账户余额:',self._balance)
21 
22     def get_balance(self):
23         return self._balance
24 
25 def main():
26     print('测试账户1:'.center(30,'*'))
27     a1=Account('Bob','5002311',20000)
28     a1.deposit(5000)
29     a1.withdraw(4000)
30     a1.info()
31 
32     print()
33 
34     print('测试账户2:'.center(30,'*'))
35     a2=Account('Joe','5006692',20000)
36     a2.withdraw(10000)
37     a2.withdraw(5000)
38     a2.info()
39 
40 if __name__=='__main__':
41     main()

task2

 1 class Shape:
 2     def info(self):
 3         pass
 4 
 5     def area(self):
 6         pass
 7 
 8     def perimeter(self):
 9         pass
10 
11 
12 class Rect(Shape):
13     def __init__(self,x=0,y=0,length=2,width=1):
14         self._x=x
15         self._y=y
16         self._width=width
17         self._length=length
18 
19     def info(self):
20         print(f'矩形左上角顶点坐标:({self._x},{self._y})')
21         print(f'矩形长:{self._length}')
22         print(f'矩形宽:{self._width}')
23 
24     def area(self):
25         return self._length*self._width
26 
27     def perimeter(self):
28         return (self._length+self._width)*2
29 
30 class Circle(Shape):
31     def __init__(self,x=0,y=0,radius=1):
32         self._x=x
33         self._y=y
34         self._r=radius
35 
36     def info(self):
37         print(f'圆心:({self._x},{self._y})')
38         print(f'半径:{self._r}')
39 
40     def area(self):
41         return 3.14*self._r*self._r
42 
43     def perimeter(self):
44         return 2*3.14*self._r
45 
46 
47 class Triangle(Shape):
48     def __init__(self,a=1,b=1,c=1):
49         self._a,self._b,self._c=a,b,c
50 
51     def info(self):
52         print(f'三角形三边长:({self._a},{self._b},{self._c})')
53 
54     def area(self):
55         s=(self._a+self._b+self._c)/2
56         ans=(s*(s-self._a)*(s-self._b)*(s-self._c))**0.5
57 
58         return ans
59 
60     def perimeter(self):
61         return(self._a+self._b+self._c)
62 
63 
64 def main():
65     print('测试1:'.center(40,'*'))
66 
67     shapes_lst1=[Circle(),Rect(),Triangle()]
68 
69     for t in shapes_lst1:
70         t.info()
71         print(f'面积:{t.area():.2f}')
72         print(f'周长:{t.perimeter():.2f}')
73         print()
74 
75     print('测试2:'.center(40,'*'))
76 
77     shapes_lst2=[Circle(x=2,y=2,radius=10),
78                  Rect(x=50,y=50,length=10,width=5),
79                  Triangle(a=3,b=4,c=5)]
80 
81     for t in shapes_lst2:
82         t.info()
83         print(f'面积:{t.area():.2f}')
84         print(f'周长:{t.perimeter():.2f}')
85         print()
86 
87 
88 if __name__=='__main__':
89     main()

task2_2.py

 1 from shape import Rect, Circle\
 2 
 3 
 4 shape_lst = [Rect(5, 5, 10, 5), Circle(), Circle(1, 1, 10)]
 5 
 6 for i in shape_lst:
 7     i.info()
 8     print(f'面积: {i.area(): .2f}')
 9     print(f'周长: {i.perimeter(): .2f}')
10     print()
 1 from shape import Rect, Circle\
 2 
 3 
 4 shape_lst = [Rect(5, 5, 10, 5), Circle(), Circle(1, 1, 10)]
 5 
 6 for i in shape_lst:
 7     i.info()
 8     print(f'面积: {i.area(): .2f}')
 9     print(f'周长: {i.perimeter(): .2f}')
10     print()

task3

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

 

标签:__,10,self,实验,._,print,def
From: https://www.cnblogs.com/simple77/p/17477420.html

相关文章

  • 实验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){......
  • Python实验课7
    实验任务1classAccount:'''一个模拟银行账户的简单类'''def__init__(self,name,account_number,initial_amount=10):'''构造新账户'''self._name=nameself._card_no=account_numb......
  • 实验六
    task1-11fromturtleimport*2defmove(x,y):3penup()4goto(x,y)5pendown()67defdraw(n,size=100):8foriinrange(n):9fd(size)10left(360/n)11defmain():12pensize(2)13pencolor('re......
  • 实验7 面向对象编程与内置模块
    实验任务1#task1源码1'''2银行账号3数据:持卡人姓名、账号、当前余额4操作:取款、存款、打印账户信息、返回账户余额5'''67classAccount:#一个模拟银行账户的简单类89def__init__(self,name,account_number,initial_amount=10):#构......
  • 实验七
    任务1classAccount:def__init__(self,name,account_number,initial_amount=10):self._name=nameself._card_no=account_numberself._balance=initial_amountdefdeposit(self,amount):self._balance+=amount......
  • 实验7
    任务1classAccount:def__init__(self,name,account_number,initial_amount=10):self._name=nameself._card_no=account_numberself._balance=initial_amountdefdeposit(self,amount):self._balance+=amount......
  • 实验7
    task4编程代码#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;intx=0;charch;if((fp=fopen("d:\\data4.txt","r"))==NULL){printf("Failtoinitialize\n");e......
  • 实验七
    TASK4#include<stdio.h>intmain(){charch;intcount=0;FILE*fp;fp=fopen("D:\data4.txt","r");if(fp==NULL){printf("failtoopenfile\n");......
  • 实验7
    task1.py1classAccount:2'''一个模拟银行账户的简单类'''3def__init__(self,name,account_number,initial_amount=10):4'''构造新账户'''5self._name=name6self._ca......