基础
读程序,总结程序的功能:
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和21的倍数,7和21的倍数,他们的倍数和
编程实现(for和while各写一遍):
- 求1到100之间所有数的和、平均值
sum1 = 0
for num in range(1, 101):
sum1+=num
print(sum1)
print(sum1/num)
5050
50.5
n=1
sum2=0
while n<=100:
sum2+=n
n+=1
print(sum2)
print(sum2/n)
5050
50.5
- 计算1-100之间能3整除的数的和
sum1 = 0
for num in range(100):
if num%3==0:
sum1+=num
print(sum1)
1683
n=1
sum1=0
while n<=100:
if n%3==0:
sum1+=n
if n==100:
break
n+=1
print(sum1)
1683
- 计算1-100之间不能被7整除的数的和
sum1 = 0
for num in range(1,101):
if num%7!=0:
sum1+=num
print(sum1)
4315
n=1
sum1=0
while n<=100:
if not (n%7)==0:
sum1+=n
if n==100:
break
n+=1
print(sum1)
4315
网友评论