1.
def getPentagonalNumber():
for i in range(1,101):
a=i*(3*i-1)/2
if i%10==0:
print("\n")
else:
print("%d"%a,end=" ")
getPentagonalNumber()
2.
def sumDigits(n):
ge = int(n) % 10
c = 0
for i in range(len(str(n))):
bai = int(n) // (10 * (10 ** i)) % 10
c += bai
sum = c + ge
print("这个整数之和是:%d"%sum)
sumDigits(1234)
3.
def lll(a,b,c):
d=[a,b,c]
d.sort()
print(d)
def start():
a=float(input("请输入第一个整数"))
b=float(input("请输入第二个整数"))
c=float(input("请输入第三个整数"))
lll(a,b,c)
start()
4.
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.
def printChars():
for i in range(73,91):
print(chr(i),end=" ")
if i%9==0:
print("\n")
printChars()
6.
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.
import math
def distance():
x1=float(input("请输入x1的值:"))
y1=float(input("请输入y1的值:"))
x2=float(input("请输入x2的值:"))
y2=float(input("请输入y2的值:"))
distance=math.sqrt(((x1-y1)**2+(x2-y2)**2))
print("两点之间的距离为:%.2f"%distance)
distance()
8.
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.
def tiem():
import time
ticks = time.time()*60
localtime = time.localtime(time.time())
print ("本地时间为 :", localtime)
print ("当前时间毫秒:", ticks)
tiem()
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("你赢了")
1.
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.
class Account(object):
def __init__(self):
self._id = 0
self._balance = 100
self._annuallnteresterRate = 0
@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 = Account.MonthlyIInterestRate
print('月利率是>>',tom.MonthlyIInterestRate)
tom.MonthlyInterest = Account.MonthlyInterest
print('月利息是>>',tom.MonthlyInterest)
if __name__ == "__main__":
main()