练习题

------------恢复内容开始------------

1.使用 while 循环输出 1 2 3 4 5 6   8 9

 

1 n = 1
2 while n <10 :
3     if n == 7:
4         pass
5     else:
6         print(n)
7     n = n + 1

 

2.求1-100之间所有数的和

1 n = 0
2 v = 0
3 while n < 101:
4     v = v + n
5     n = n + 1
6 print(v)

3.求1-100之间的所有偶数

 

1 n = 0
2 while n < 100:
3     if n % 2 == 0:
4         print(n)
5     else:
6         pass
7      n = n + 1

4.求1-100之间的所有奇数

1 n = 0
2 while n < 100:
3     if n % 2 == 0:
4         pass
5     else:
6         print(n)
7      n = n + 1

5. 求1-2+3-4+5....99的和

 

1 n = 1
2 v = 0
3 while n < 100:
4     if n % 2 == 0:
5         v = v - n
6     else:
7         v = v + n
8     n = n + 1
9 print(v)

 

6.用户登陆3次机会

 1 n = 0
 2 while n < 3:
 3     user_name = input('请输入用户名:')
 4     password = input('请输入密码:')
 5     if user_name== 'root' and password == 'root':
 6         print('欢迎访问')
 7         break #跳出循环
 8     else:
 9         print('密码或用户名错误')
10         n = n + 1
11 else:
12     print('用户名和密码错误超过三次,')

你可能感兴趣的:(练习题)