while num<=100: sum_all += num if num % 2 == 0: sum_even += num else: sum_odd += num num += 1 print("1-100所有数的累加和",sum_all) print("1-100所有偶数的累加和",sum_even) print("1-100所有奇数的累加和",sum_odd) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12....
= 0: #奇数 sum_odd += i #奇数和 else: #偶数 sum_even += i #偶数和print("1~100中所有奇数的和:", sum_odd)print("1~100中所有偶数的和:", sum_even) 1. 程序执行结果如下: C:\Users\zemuerqi\PycharmProjects\Python2020\venv\Scripts\python.exe C:/Users/zemuerqi/PycharmProjects/Pytho...
encoding: utf-8# Python 3.9.0n=1s=0while n<=100: if n % 2 == 0: s=s+n n=n+1print(s)
第一个的if continue实际上没用,没控制到任何代码 第二个貌似是奇数相加
练习2:求1~100中所有能被3整除但是不能被7整除数的和 s1=0 for i in range(1,101): if i%3==0: if i%7!=0: s1=s1+i print(s1) 1. 2. 3. 4. 5. 6. 2、统计个数 案列:统计num中偶数的个数 scores=[78,34,56,90,78,89,88,60,91] ...
while i<=100: result += i # result = result + i i += 1 print(result) ''' 1. 2. 3. 4. 5. 6. 7. 8. 应用二:1-100的偶数累加和 2+4+6+...+100 方法一:加入判断语句,i/2=0则为偶数,偶数则累加 1、准备做加法运算的数据 1-100 增量为1 ...