- 读程序,总结程序功能
numbers=1
for i in range(0,20):
numbers*=2
print(numbers)
求出2的20次方,结果为1048576
summation=0
num=1
while num<=100:
if (num%3==0 or num%7==0) and num%21!=0:
summation += 1
num+=1
print(summation)
找出1~100之间,能够被3或者7整除,并且不被21整除的数,输出符合要求的数字的个数。
- 编程实现(for和while各写一遍):
求1到100之间所有数的和、平均值
for 循环
sum = 0
for x in range(1,101):
sum += x
sum1 = sum / 100
print(sum,sum1)
while 循环
sum = 0
x = 1
while x <= 100:
sum += x
x += 1
sum1 = sum / 100
print(sum,sum1)
结果
5050 50.5
计算1-100之间能3整除的数的和
for 循环
sum = 0
for x in range(1,101):
if x % 3 == 0:
sum += x
print(sum)
while 循环
sum = 0
x = 1
while x <= 100:
if x % 3 == 0:
sum += x
x += 1
print(sum)
结果
1683