#1.编写一个测试程序来使用这个函数显示前100个五角星,每行显示10个。
def getPentagonalNumber(n):
sum = 0
for i in range(1,n+1):
num = i*(3*i-1)/2
print(int(num),end=' ')
sum += 1
if sum %10 == 0:
print("\n")
getPentagonalNumber(100)
#2.编写程序提示用户输入一个整数,然后显示这个整数所有数字的和。
def sumDigits(n):
gewei = int(n) % 10
c = 0
for i in range(len(str(n))):
baiwei = int(n) // (10 * (10**i)) % 10
c += baiwei
sum = c+gewei
print("这个整数所有数字的和%d"%sum)
sumDigits(234)
#3.编写一个测试程序,提示用户输入三个整数,然后调用函数按升序显示三个数。
def displaySortedNumbers(num1,num2,num3):
list = [num1,num2,num3]
num = sorted(list)
print("the sorted numbers are {}".format(num))
if __name__ == "__main__":
a,b,c = map(int,input("enter three number:").split(","))
displaySortedNumbers(a,b,c)
#4.编写一个程序提示用户输入投资额和百分比格式的年利率,然后输出一份表格显示年份从1到30年的未来值。
from prettytable import PrettyTable
list = []
def futureInvestmentValue(inAmount,rate,years):
for i in range(1,years + 1):
futureInvestment = inAmount + ((1 +rate) ** (12 * i))
list.append([i,futureInvestment])
table = PrettyTable(['year','Future Value'])
for row in list:
table.add_row(row)
print(table)
if __name__ == "__main__":
inAmount = int(input("请输入投资额:"))
rate = float(input("请输入百分比格式的年利率:")) / 12
futureInvestmentValue(inAmount,rate,years = 30)
#5.编写一个测试程序,打印“l”到“z”的字符,每行打印10个
def printChars():
for i in range(73,91):
print(chr(i),end=" ")
if i%9==0:
print("\n")
printChars()
#6.编写一个测试程序,显示从2010年到2020年每年的天数。
def numberOfDaysInAYear(year):
for count in range(year,year+11):
if count % 4 == 0 and count % 100 != 0 or count % 400 == 0:
print("{}年有366天".format(count))
else:
print("{}年有365天".format(count))
numberOfDaysInAYear(2010)
#7.使用下面的函数计算两点之间的距离。
def distance(x1,y1,x2,y2):
distance = ((x2-x1)**2 + (y2-y1)**2)**0.5
print ("这两点间的距离是:%f"%distance)
distance(1,4,4,2)
#8.编写程序找出所有p<=31的梅森素数。
from prettytable import PrettyTable
def mei(p):
c = []
b = []
for p in range(2,32):
if p>1:
for i in range(2,p):
if (p % i) == 0:
#print(p,"不是质数")
#print(i,"乘于",p//i,"是",p)
break
else:
#print(p,"是质数")
d = 2**(p-1)
c.append([p,d])
for x in c:
if x not in b:
b.append(x)
table = PrettyTable(['p','2**(p-1)'])
for row in b:
table.add_row(row)
print(table)
mei(5)
#9.编写程序显示日期和时间。
import time
localtime = time.asctime(time.localtime(time.time()))
print("本地时间为 :", localtime)
#10.编写程序玩
import random
def shaizi():
a=random.choice([1,2,3,4,5,6])
b=random.choice([1,2,3,4,5,6])
if a+b==2 or a+b==3 or a+b==12:
print('%d + %d = %d' %(a,b,a+b))
print('你输了')
elif a+b==7 or a+b==11:
print('%d + %d = %d' %(a,b,a+b))
print('你赢了')
else:
print('%d + %d = %d' %(a,b,a+b))
c=random.choice([1,2,3,4,5,6])
d=random.choice([1,2,3,4,5,6])
if c+d==7:
print('%d + %d = %d' %(c,d,c+d))
print('你输了')
elif c+d==a+b:
print('%d + %d = %d' %(c,d,c+d))
print('你赢了')
shaizi()
#1.编写一个测试程序创建两个Rectangle对象,一个宽4高为40,而另一个宽为3.5
高为35.7。按照这个顺序显示每个矩形的宽、高、面积和周长。
class Rectangle(object):
def __init__(self,width=1,height=2):
self.width = width
self.height = height
def getArea(self):
return self.width*self.height
def getPerimeter(self):
return (self.width+self.height)*2
def main():
rectangle = Rectangle(4,40)
print('宽是>>',rectangle.width)
print('高是>>',rectangle.height)
print('面积是>>',rectangle.getArea())
print('周长是>>',rectangle.getPerimeter())
print('---------------------------------')
tom = Rectangle(3.5,35.7)
print('宽是>>',tom.width)
print('高是>>',tom.height)
print('面积是>>',tom.getArea())
print('周长是>>',tom.getPerimeter())
if __name__ == "__main__":
main()
#2.编写测试程序创建一个Account对象,这个账户的id是1122,账户额是20000美元而年
利率是4.5%,使用withdraw方法取2500美元,使用deposit方法存3000美元,并打印id、金额、月利率和月利息
class Account(object):
def __init__(self,id=0,balance=100,annuallnteresterRate=0):
self._id = id
self._balance = balance
self._annuallnteresterRate = annuallnteresterRate
@property
def id(self):
return self._id
@id.setter
def id(self,new_id):
self._id = new_id
@property
def balance(self):
return self._balance
@balance.setter
def balance(self,new_balance):
self._balance = new_balance
@property
def annuallnteresterRate(self):
return self._annuallnteresterRate
@annuallnteresterRate.setter
def annuallnteresterRate(self,new_annuallnteresterRate):
self._annuallnteresterRate = new_annuallnteresterRate
def MonthlyIInterestRate(self,new_id,new_balance,new_annuallnteresterRate):
return new_annuallnteresterRate /12
def MonthlyInterest(self,new_id,new_balance,new_annuallnteresterRate):
return new_balance*new_annuallnteresterRate/12
def withdraw(self,new_id,new_balance,new_annuallnteresterRate):
print('取出金额为:')
def deposit(self,new_id,new_balance,new_annuallnteresterRate):
print('充进金额为:')
def main():
tom = Account()
tom.id = 1122
tom.balance = 20000
tom.annuallnteresterRate = 0.45
tom.withdraw = 2500
tom.deposit = 3000
print('此账号id是>>',tom.id)
a = tom.balance -tom.withdraw+tom.deposit
print('余额是>>',a)
tom.MonthlyIInterestRate = tom.annuallnteresterRate/12
print('月利率是>>',tom.MonthlyIInterestRate)
tom.MonthlyInterest = a*tom.MonthlyIInterestRate
print('月利息是>>',tom.MonthlyInterest)
if __name__ == "__main__":
main()
#3.编写测试程序创建两个fan对象,对第一个对象赋值最大速度,半径为10,颜色为黄色,打开它
#对第二个对象赋值中速,半径为5,颜色为蓝色,关闭它。显示每个对象的speed\radius\color\on的属性
class Fan(object):
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"
elif self.__speed == 3:
self.__speed=new_speed = "FAST"
@property
def on(self):
return self.__on
@on.setter
def on(self,new_on):
self.__on==new_on
@property
def radius(self):
return self.__radius
@radius.setter
def radius(self,new_radius):
self.__radius==new_radius
@property
def color(self):
return self.__color
@color.setter
def color(self,new_color):
self.__color==new_color
def fans(self):
print("{}颜色的风扇半径为{},速度{},打开状态{}".format(self.__color,self.__radius,self.__speed,self.__on))
if __name__ == '__main__':
lulu = Fan()
lulu.fans()
lulu1 = Fan(3,on=True,radius=10.0,color="yellow")
lulu1.fans()
lulu1 = Fan(2,on=False,radius=5.0,color="blue")
lulu1.fans()
#4.编写测试程序创建三个RegularPolygon对象,这三个对象是分别使用RegularPolygon(),RegularPolygon(6.4),RegularPolygon(10,4,5.6,7.8)
#来创建的。对于每个对象,显示它的周长和面积
import math
class RegularPolygon(object):
def __init__(self,n=3,length=1,x = 0,y = 0):
self.__n = n
self.__length = length
self.__x = x
self.__y = y
def N(self):
return self.__n
def side(self):
return self.__length
def getPerimeter(self):
perimeter = self.__n * self.__length
return perimeter
def getArea(self):
area = round((self.__n * self.getPerimeter() * self.getPerimeter()) /( 4 * math.tan(math.pi/4)),2)
return area
def main():
Re = RegularPolygon(4,1,8,0)
a = Re.getPerimeter()
b = Re.getArea()
n = Re.N()
length = Re.side()
print("这是一个",n,"边形","边长是",length,"周长为",a,"面积为",b)
main()
#5.编写测试程序提示用户输入a,b,c,d,e,f的值,然后显示结果。
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()
#6.编写程序提示用户输入这四个端点,然后显示它们的交点
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.jisuan(x1,y1,x2,y2,x3,y3,x4,y4)
def jishuan(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.gongshi(a,b,c,d,e,f)
def gongshi(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()
homework04
最新推荐文章于 2020-10-30 23:51:12 发布