计算1-100数之和:
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #计算1-100数之和
4 n = 1
5 s = 0
6 while n < 101:
7 s = s + n
8 n = n + 1
9 print(s)
输出012345689数字
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #输出012345689数字
4 n = 0
5 while n < 10:
6 if n == 7:
7 pass
8 else:
9 print(n)
10 n = n + 1
11 print('---end---')
输出1-100以内的所以奇数
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #输出1-100以内的所有奇数
4 n = 1
5 while n < 101:
6 temp = n % 2
7 if temp == 0:
8 pass
9 else:
10 print(n)
11 n = n + 1
12 print('---end---')
#!/usr/bin/env python
# -*- coding:utf8 -*-
#输出1-100以内所有奇数
n = 1
while n < 101:
temp = n % 2
if temp != 1:
pass
else:
print(n)
n = n + 1
print('---end---')
输出1-100以内所有偶数
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #输出1-100以内所有偶数
4 n = 1
5 while n < 101:
6 temp = n % 2
7 if temp != 0:
8 pass
9 else:
10 print(n)
11 n = n + 1
12 print('---end---')
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #输出1-100以内所有偶数
4 n = 1
5 while n < 101:
6 temp = n % 2
7 if temp == 1:
8 pass
9 else:
10 print(n)
11 n = n + 1
12 print('---end---')
求1-2+3-4+5...+99之和
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #求1-2+3-4+5...+99之和
4 n = 1
5 s = 0
6 while n < 100:
7 temp = n % 2
8 if temp == 0:
9 s = s-n
10 else:
11 s = s + n
12 n = n + 1
13 print(s)