类的练习
class Rectangle(object):
def __init__(self):
pass
def rose (self):
self.width = float(input('width :'))
self.height = float(input('height :'))
def getArea (self):
Area = self.width * self.height
print ('面积为: %2.f' % Area)
def getPerimeter (self):
perimeter = (self.width + self.height)* 2
print ('周长为: %2.f'% perimeter)
if __name__=='__main__':
Joker = Rectangle()
Joker.rose()
Joker.getArea()
Joker.getPerimeter()
不完整
class Account(object):
def __init__(self,id_=0,balance=100,annuallntersetRate=0):
self.__id = id_
self.__balance = balance
self.__annuallntersetRate = annuallntersetRate
@property
def id(self):
return self.__id
@id.setter
def id(self,new_id):
self.__id = new_id
def getMonthlyInterestRate(self):
return self.__annuallntersetRate / 12 / 100
def getMonthlyInterestRate(self):
rate = self.genMonthlyInterestRate()
res = self.__balance * rate
return res
def withdraw(self,money):
if self.__balance >= money:
self.__balance -= money
print('您以成功取出:%2.f'% money)
print('您的余额为:%2.f'% self.__balance)
else:
print('您的余额不足')
print('您的余额为:%2.f'% self.__balance)
def deposit(self,money):
self._balance += money
print('您以成功存入:%2.f'% money)
print('您的余额为:%2.f'% self.__balance)
if if __name__ == "__main__":
account = Account(id_=1122,balance=2000,annuallntersetRate=4.5)
print(account.getMonthlyInterestRate())
class Fan():
def __init__(self,speed=1,on=False,radius=5.0,color="blue"):
self.__speed = int(speed)
self.__on = bool(on)
self.__radius = float(radius)
self.__color = str(color)
@property
def speed(self):
return self.__speed
@speed.setter
def speed(self,new_speed):
if self.__speed == 1:
self.__speed=new_speed = "SLOW"
elif self.__speed == 2:
self.__speed=new_speed = "MEDIUM"
else:
self.__speed=new_speed = "FAST"
@property
def on(self):
return self.__on
@property
def radius(self):
return self.__radius
@property
def color(self):
return self.__color
def fans(self):
print("{}颜色的风扇半径为{},速度{},打开状态{}".format(self.__color,self.__radius,self.__speed,self.__on))
if __name__ == '__main__':
ynn = Fan()
ynn.fans()
ynn1 = Fan(3,on=True,radius=10.0,color="yellow")
ynn1.fans()
ynn1 = Fan(2,on=False,radius=5.0,color="blue")
ynn1.fans()
import math
class LinearEquation(object):
def __init__(self):
pass
def main(self):
n=float(input('输入边数:'))
side = int(input('输入边长:'))
self.isSolvable(n,side)
def isSolvable(self,n,side):
L = n*side
Area = float((n*side**2)/4*math.tan(math.pi/n))
print('周长为:',L,'面积为:%.2f'%Area)
if __name__ == "__main__":
linearequation = LinearEquation()
linearequation.main()
class LinearEquation(object):
def __init__(self):
pass
def main(self):
a = float(input('a= '))
b = float(input('b= '))
c = float(input('c= '))
d = float(input('d= '))
e = float(input('e= '))
f = float(input('f= '))
self.isSolvable(a,b,c,d,e,f)
def isSolvable(self,a,b,c,d,e,f):
if a*d-b*c ==0:
print('此方程无解')
else:
x = (e*d-b*f)/(a*d-b*c)
y = (a*f-e*c)/(a*d-b*c)
print('x为:',x)
print('y为:',y)
if __name__ == "__main__":
linearequation = LinearEquation()
linearequation.main()
7.
class LinearEquation(object):
def __init__(self):
pass
def main(self):
x1,y1=eval(input('请输入第一条直线的第一个端点(逗号分隔)'))
x2,y2=eval(input('请输入第一条直线的第二个端点(逗号分隔)'))
x3,y3=eval(input('请输入第二条直线的第一个端点(逗号分隔)'))
x4,y4=eval(input('请输入第二条直线的第二个端点(逗号分隔)'))
self.math(x1,y1,x2,y2,x3,y3,x4,y4)
def math(self,x1,y1,x2,y2,x3,y3,x4,y4):
a = y2-y1
b = x2*y1-x1*y2
c = x2-x1
d = y4-y3
e = x4*y3-x3*y4
f = x4-x3
self.isSolvable(a,b,c,d,e,f)
def isSolvable(self,a,b,c,d,e,f):
y = float(a*e-b*d)/(a*f-c*d)
x = float(y*c-b)/a
print('交点的横纵坐标为:',(x,y))
if __name__ == "__main__":
linearequation = LinearEquation()
linearequation.main()
函数的练习
1.
def rose ():
n = 1
t = 0
for i in range(100):
i = n*(3*n-1)/2
n += 1
print(i,end = ' ')
t += 1
if t %10 ==0:
print('\n')
rose()
2.
def rose (a):
bai = a//100%10
shi = a//10%10
ge = a%10
print(bai+shi+ge)
def rose1():
a = int(input('请输入一个百位数'))
rose(a)
rose1()
3.
def rose (a):
sorted(a)
print(a)
def rose1():
a = []
for i in range(3):
i = int(input('请输入三个整数'))
a.append(i)
rose(a)
rose1()
4.
def futureInvestmenValue(investmentAmount,AnnualInterestRate):
monthlyInterestRate = float(AnnualInterestRate)/12/100
print("Years","\t","Future Value")
for years in range(1,31):
futureValue = investmentAmount * ((1 + monthlyInterestRate)**(12*years))
print(years,"\t","%.2f"%futureValue)
if __name__ == '__main__':
investmentAmount = eval(input("The amount invested:"))
AnnualInterestRate = eval(input("Annual interest rate:"))
futureInvestmenValue(investmentAmount,AnnualInterestRate)
5.
def printChars():
ch1 = ['1','2','3','4','5','6','7','8','9']
ch2 = ['Q', 'E','N','B','V','C','X','H','S','Z']
for i in ch2:
ch1.append(i)
count = 0
for j in ch1:
count +=1
if 'Z' in ch1:
print(j,end = ' ')
if count%10 ==0:
print()
printChars()
6.
def printChars(ch1,ch2,numberPerLine):
count = 0
for i in range (ch1,ch2):
numberPerLine = chr(i)
count +=1
print(numberPerLine,end = " ")
if count % 10 ==0:
print("\n")
if __name__ == '__main__':
ch1 = 73
ch2 = 91
numberPerLine = ""
printChars(ch1,ch2,numberPerLine)
7.
def distance():
x1,y1 = eval(input("Enter x1 and y1 for Point1:"))
x2,y2 = eval(input("Enter x2 and y2 for Point2:"))
distance = ((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) ** 0.5
print("The distance between the two points is",distance)
distance()
8.
def meisen():
print("2",3)
for i in range(3,32,1):
if i%2!=0 and i!=25:
if i%3!=0 or i==3:
sum=2**i-1
print(i,sum ,end=' ')
print()
meisen()
9.
def tiem():
import time
ticks = time.time()*60
localtime = time.localtime(time.time())
print ("本地时间为 :", localtime)
print ("当前时间毫秒:", ticks)
tiem()
10.
import random
a1 = random.randint(1,6)
a2 = random.randint(1,6)
a3 = a1+a2
if a3==2 or a3==3 or a3==12:
print("你输了")
elif a3==7 or a3==11:
print("你赢了")
else:
for i in range(1,1000):
a4 = random.randint(1,6)
a5 = random.randint(1,6)
a6 = a5+a4
if a6 == 7:
print("你输了")
break
elif a6 == a3:
print("你赢了")