基础 读程序,总结程序的功能:
-
numbers=1
for i in range(0,20):
numbers*=2
print(numbers)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)
39
编程实现(for和while各写一遍):
- 求1到100之间所有数的和、平均值
num=0
while num<=100:
sum+=num
num+=1
print(sum)
ave=sum/100
print(ave)
for x in range(101):
sum+=x
print(sum)
ave=sum/100
print(ave)
- 计算1-100之间能3整除的数的和
sum=0
for x in range(101):
sum+=x
print(sum)
ave=sum/100
print(ave)
num=0
while num<=100:
sum+=num
num+=1
print(sum)
- 计算1-100之间不能被7整除的数的和
```python
sum=0
for x in range(1,101):
if(x % 7!=0):
sum+=x
print(sum)
网友评论