讀程序碎捺,總結(jié)程序的功能:
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以內(nèi)能整除3或乘除7并且不整除21的數(shù)的個(gè)數(shù)
編程實(shí)現(xiàn)(for和while各寫?遍):
- 求1到100之間所有數(shù)的和苍苞、平均值
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)
- 計(jì)算1-100之間能3整除的數(shù)的和
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)
- 計(jì)算1-100之間不能被7整除的數(shù)的和
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)