练习—8.17 while循环
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了练习—8.17 while循环相关的知识,希望对你有一定的参考价值。
练习题:
1. 打印 1-99之间的所有奇数(循环,每个数递增)
方法一:
start = 1
while start <= 99:
n = start % 2
if n == 1:
print(start)
start = start + 1
else:
start = start + 1
方法二:
start = 1
while start <= 99:
n = start % 2
if n == 1:
print(start)
start = start + 1
2. 打印 1-99之间所有能被3整除数
start = 1
while start <= 99:
n = start % 3
if n == 0:
print(start)
start = start + 1
3. 打印 1-99之间所有数总和
start = 1
num = 0
while start <= 99:
num = num + start
start = start + 1
print(num)
4. 打印 1-99之间所有奇数总和
start = 1
num = 0
while start <= 99:
n = start % 2
if n == 1:
num = num + start
start = start + 1
print(num)
5. 打印 1-2+3-4+5-6+7 ...+99 总和
start = 1
num = 0
while start <= 99:
n = start % 2
if n == 1:
num = num + start
else:
num = num - start
start = start + 1
print(num)
以上是关于练习—8.17 while循环的主要内容,如果未能解决你的问题,请参考以下文章