编程练习

计算1到100以内能被3或者7整除,但不能同时被3和7整除的数的个数,运行结果为39

任务

  1. 定义变量num用来存放数值,count用来存放个数
  2. 使用while实现循环
  3. 通过if来设置符合的条件,符合条件count计数加1
  4. 输出符合条件的总个数count

任务提示

  • 同时被3和7整除,即为21的倍数;
  • 除以21不等于0即不能同时被3和7整除

初始代码

num = 1
count = 0
# 循环条件
# 循环体,设置条件
# 补全代码
num = num + 1
print(count)
hosted with ❤ by GitHub

代码提交区

# coding:utf-8
num = 1
count = 0
# 循环条件
while num < 100:
# 循环体,设置条件
# 补全代码
if num % 3 == 0 or num % 7 == 0:
count += 1
if num % 21 == 0:
count -= 1
num = num + 1
print(count)
hosted with ❤ by GitHub
# coding:utf-8
num = 1
count = 0
# 循环条件
while num < 100:
# 循环体,设置条件
# 补全代码
if num % 3 == 0 or num % 7 == 0:
if num % 21 != 0:
count += 1
num = num + 1
print(count)
hosted with ❤ by GitHub