9.1 使用while循环输出 1 2 3 4 5 6 8 9 10
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3
4 n = 1;
5
6 while n < 11 :
7 if n == 7 :
8 pass
9 else :
10 print(n)
11 n = n + 1
9.2 求1-100的和
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3
4 n = 1
5 s = 0
6 while n < 101 :
7 s = s + n
8 n = n + 1
9 print(s)
9.3 输出 1-100 内的所有奇数
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3
4 n = 1
5
6 while n < 101 :
7 if n % 2 == 1 :
8 print(n)
9 n = n + 1
9.4 输出 1-100 内的所有偶数
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3
4 n = 1
5
6 while n < 101 :
7 if n % 2 == 0 :
8 print(n)
9 n = n + 1
9.5 求1-2+3-4+5 ... 99的和
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3
4 n = 1
5 s = 0
6
7 while n < 100 :
8 if n % 2 == 0 :
9 s = s - n
10 else :
11 s = s + n
12 n = n + 1
13 print(s)
9.6 前面的用户登录(可以有三次登录机会)
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3
4 print("你有三次输入机会")
5
6 n = 1
7
8 while n < 4 :
9 name = input("请输入用户名:")
10 password = input("请输入密码:")
11
12 if name == "root" :
13 if password == "123456" :
14 print("登录成功")
15 break
16 else :
17 print("第" , n , "次,密码错误")
18 else :
19 print("第" , n , "次,用户名错误")
20 if n == 3 :
21 print("你已输错三次,异常退出")
22 n = n + 1