标签:art else 之间 start 方法 int 个数 打印 ==
练习题:
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)
标签:art else 之间 start 方法 int 个数 打印 ==
原文地址:http://www.cnblogs.com/xu1686318405/p/7502952.html