sum = 0
for i in range(1, 11, 1):
a = 1
for b in range(1, i + 1):
a = a * b
sum = sum + a
print("%d" % sum)
结果即:
2· 使用for循环嵌套语句打印下三角形式的九九乘法表.
for i in range(1, 10):
for j in range(1, i+1):
print('{}*{}={}'.format(i, j, i*j), end=' ')
print("")
结果即:
3·使用for循环嵌套语句打印100以内的所有素数import math
for num in range(2, 101):
flag = True
for i in range(2, int(math.sqrt(num)) + 1):
if num % i == 0:
flag = False
break
if flag is True:
print(num, end=" ")
结果即:
4·使用for循环嵌套语句打印100以内的所有素数(每行打印输出5个素数)import math
count = 0
for num in range(2, 101):
flag = True
for i in range(2, int(math.sqrt(num)) + 1):
if num % i == 0:
flag = False
break
if flag is True:
print(num, end=" ")
count += 1
if count % 5 == 0:
print()
结果即:
5·在一行内打印输出由1、2、3、4这4个数字组成的每位数都不相同的所有三位数
for i in range(1, 5):
ii = i*100
for j in range(1, 5):
if j != i:
jj = j*10
for k in range(1, 5):
if k != j and k != i:
print(ii+jj+k, end=" ")
结果即:
6·在一行内打印输出1000以内的所有完数(完数:因子之和等于自身,比如:6就是完数,因为6=1+2+3)for i in range(1, 1001):
sum = 0
for j in range(1, i):
if i % j == 0:
sum = sum+j
if sum == i:
print(i, end=",")
结果即:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)