练习题:
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)