首页 > 编程语言 > 详细

Python基础-----while循环练习

时间:2018-08-18 13:42:58      阅读:167      评论:0      收藏:0      [点我收藏+]

一、使用while循环输出1 2 3 4 5 6   8 9 10

 1 #!/usr/bin/evc python 3
 2 # -*- coding:utf-8 -*-
 3 
 4 ‘‘‘
 5 使用while循环输出1 2 3 4 5 6   8 9 10
 6 ‘‘‘
 7 
 8 count = 1
 9 while count<=10:
10     if count == 7:
11         pass
12     else:
13         print(count)
14     count += 1

二、使用while循环输出1~100的和

 1 #!/usr/bin/evc python 3
 2 # -*- coding:utf-8 -*-
 3 
 4 ‘‘‘
 5 使用while循环输出1~100的和
 6 ‘‘‘
 7 
 8 i = 1
 9 sum = 0
10 while i<=100:
11     sum = sum + i
12     i += 1
13 print(sum)

三、使用while循环输出1~100内所有的奇数

#!/usr/bin/evc python 3
# -*- coding:utf-8 -*-

‘‘‘
使用while循环输出1~100内所有的奇数
‘‘‘
i = 0
while i < 100:
    i+=1
    while i % 2 != 0:
        print(i)
        i += 1

四、使用while循环输出1~100内所有的偶数

#!/usr/bin/evc python 3
# -*- coding:utf-8 -*-

‘‘‘
使用while循环输出1~100内所有的偶数
‘‘‘
i = 0
while i < 100:
    i+=1
    while i % 2 == 0:
        print(i)
        i += 1

五、使用while循环输出1-2+3-4+5.....+99的和

 1 #!/usr/bin/evc python 3
 2 # -*- coding:utf-8 -*-
 3 
 4 ‘‘‘
 5 使用while循环输出1-2+3-4+5.....+99的和
 6 ‘‘‘
 7 
 8 i = 1
 9 sum = 0
10 while i<=99:
11     if i % 2 == 0:
12         sum = sum - i
13     else:
14         sum = sum + i
15     i += 1
16 print(sum)

六、判断用户登录,三次机会重试

 1 #!/usr/bin/evc python 3
 2 # -*- coding:utf-8 -*-
 3 
 4 ‘‘‘
 5 用户登录,三次机会重试
 6 ‘‘‘
 7 
 8 
 9 count = 1
10 while count <= 3:
11 
12     if input(请输入用户名:) == Liming:
13         print(登录成功!)
14         break
15     else:
16         print(请重试!)
17     count += 1
18 else:
19     print(已失败三次,今日无法登录。)

 

Python基础-----while循环练习

原文:https://www.cnblogs.com/Meanwey/p/9497046.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!