读程序,总结程序的功能:
numbers=1
for i in range(0,20):
numbers*=2
print(numbers)
2的20次方
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)
求100以内能整除3或乘除7并且不整除21的数的个数
编程实现(for和while各写⼀遍):
- 求1到100之间所有数的和、平均值
sum = 0
for x in range(1,101):
sum +=x
print(sum,sum/100)
# 5050 # 50.5
n = 1
sum = 0
while n <= 100:
sum += n
n += 1
print(sum)
- 计算1-100之间能3整除的数的和
sum = 0
for x in range(1,101):
if x % 3 == 0:
sum += x
print(sum)
sum = 0
n = 1
while n <= 100:
if n %3 == 0:
sum+=n
if n == 100:
break
n+=1
print(sum)
- 计算1-100之间不能被7整除的数的和
sum = 0
for x in range(1,101):
if x %7 != 0:
sum+=x
print(sum)
sum = 0
n = 1
while n <= 100:
if n % 7 != 0:
sum+=n
if n == 100:
break
n +=1
print(sum)