9.29 day5作业

2018-09-29  本文已影响0人  烟雨江畔丶

1.读程序,总结程序的功能:

numbers=1 
for i in range(0,20): 
 numbers*=2 
print(numbers) #结果为1048576
#求2**20的值 print(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)  #结果为39
#求1-100中是3的倍数或7的倍数同时不是21的倍数的个数。
#求1-100中是3的倍数或7的倍数同时不是21的倍数的个数

编程实现(for和while各写一遍):

  1. 求1到100之间所有数的和、平均值
num = 0
for x in range(1,101):
    num += x
print(num)
print(num) #求和 结果为:5050

num = 0
num1 = 1
for i in range(0,100):
    if num<=100:
        num += 1
        num1=num/2
        print(num1)
#求平均值结果(0.5 1.0 1.5 2.0 ....  50.0)
#改 sum1 = 0
for x in range(0,101):
    sum1 += x
print('和是:%s 平均值:%.2f' %(sum1,sum1/100))
和是:5050 平均值:50.50

num = 1
sum1 = 0
while num <= 100:  
    sum1 += num
    num += 1
print(sum1)
#求和结果为5050
num = 0
num1 = 1
for i in range(0,100):
    while num<=100:
        num += 1
        num1=num/2
        print(num1)
 #求平均值结果(0.5 1.0 1.5 2.0 ....  50.0)
  1. 计算1-100之间能3整除的数的和
num = 0
for x in range(1,101):
    if x % 3 == 0:
        num += x
print(num)

#结果1683
num = 0
for x in range(1,100):
    while x %3 == 0:
        num += x
        x+=1
print(num)#结果为 1683

3.计算1-100之间不能被7整除的数的和

for x in range(1,101):
    if x % 7 != 0:
        num += x
print(num) 
#结果4315

上一篇下一篇

猜你喜欢

热点阅读