代码:
def getPentagonalNumber(n):
if n % 10 == 0:
print()
def Start():
for n in range(1,101):
a = n * (3 * n - 1)/2
print('%d'%a,end = "\t")
getPentagonalNumber(n)
Start()
运行结果:
代码:
def sumDigits(n):
sum = 0
m = str(n)
for i in range(len(m)):
num = n//10**i%10
sum += num
print(sum)
def Start():
n = int(input("请输入一个整数:"))
sumDigits(n)
Start()
运行结果:
请输入一个整数:234
4
7
9
代码:
def displaySortedNumbers(num1,num2,num3):
c = [num1,num2,num3]
c.sort()
print(c)
def Start():
num1 = int(input('>>'))
num2 = int(input('>>'))
num3 = int(input('>>'))
displaySortedNumbers(num1,num2,num3)
Start()
运行结果:
3
6
5
[3, 5, 6]
代码:
def futureInvestmentValue(Amount,year):
m = year*12
y = 0
for i in range(m):
if i % 12 == 0:
y += 1
money = Amount*(1 + 0.05/12)**i
print("%d 年%f"%(y,money))
futureInvestmentValue(10000,20)
运行结果:
1 年10000.000000
2 年10511.618979
3 年11049.413356
4 年11614.722313
5 年12208.953550
6 年12833.586785
7 年13490.177442
8 年14180.360522
9 年14905.854679
10 年15668.466494
11 年16470.094977
12 年17312.736294
13 年18198.488741
14 年19129.557963
15 年20108.262454
16 年21137.039324
17 年22218.450372
18 年23355.188461
19 年24550.084228
20 年25806.113130
代码:
def printChars(ch1,ch2,numberPerLine):
for i in range(ch1,ch2+1):
j = chr(i)
print(j,end = " ")
if (i + 2)%10 == 0:
print()
def Start():
ch1 = 49
ch2 = 90
numberPerLine = 1
printChars(ch1,ch2,numberPerLine)
Start()
运行结果:
代码:
def add(year):
for i in range(year,year + 11):
print("%d年"%i,end = " ")
if i % 4 ==0:
print("366天")
else:
print("365天")
def Start():
year =2010
add(year)
Start()
运行结果:
代码:
import math
def distance(x1,x2,y1,y2):
l = math.sqrt((x1-x2)**2+(y1-y2)**2)
print("两点间的距离是%.2f"%l)
def Start():
x1,x2 = map(float,input('>>'))
y1,y2 = map(float,input('>>'))
distance(x1,x2,y1,y2)
Start()
运行结果:
00
51
两点间的距离是4.00
代码:
def sushu():
i = 2
c = []
d = []
while i <= 31:
j = 2
while j <=i:
if i % j == 0:
if i == j:
c.append(i)
break
j += 1
i += 1
print(c)
for p in c:
d.append(2 ** p - 1)
print(d)
sushu()
运行结果:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
[3, 7, 31, 127, 2047, 8191, 131071, 524287, 8388607, 536870911, 2147483647]
代码:
import time
localtime = time.asctime(time.localtime(time.time()))
print("本地时间为:",localtime)
运行结果:
本地时间为: Tue Aug 20 19:53:15 2019
代码:
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("你输了")
elif a+b == 7 or a+b == 11:
print("你赢了")
else:
c = random.choice([1,2,3,4,5,6])
d = random.choice([1,2,3,4,5,6])
if c+d == 7:
print("你输了")
elif c+d ==a+b:
print("你赢了")
shaizi()
运行结果:
你赢了