Python学习之While循环
来源:互联网 发布:淘宝手淘搜索提升 编辑:程序博客网 时间:2024/05/19 00:13
while循环
while循环是在高中学习过的知识,也就是高中所称的当型循环。意思就是当条件满足的时候,就执行循环体内的语句,如果不满足条件,则退出。
需求,如果年龄大于60,那么输出 hi,you shoudn’t work !,否则,输出,”Please continue work !
方法一:使用if条件实现
#!/usr/bin/env python#coding:utf-8print "Please input your age: ",age = int(raw_input())if age >= 60: print "Hi, you shouldn\'t work !"else: print "Sorry,Please continue work !"
执行结果:
[root@python ~]# python iftest1.py Please input your age: 60Hi, you shouldn't work ![root@python ~]# python iftest1.py Please input your age: 40 Sorry,Please continue work !
方法二:使用while循环
在使用while循环之前,有必要学习一下while循环的语法
while 循环语法
while condition:
statement
这是一个简单的while循环的语句,我们现在来看上面的需求该怎么实现。
#!/usr/bin/env python#coding:utf-8print "Please input your age: "age = int(raw_input())while age >= 60: print "Hi, you shoudn\'t work !" breakelse: print "Sorry,Please continue work !"
执行结果:
[root@python ~]# python whitest.py Please input your age: 60Hi, you shoudn't work ![root@python ~]# python whitest.py Please input your age: 40Sorry,Please continue work !
上面就是一个while循环,但是,我们再里面增加了关键字break和else才能实现需求,但是我们也可以这么干。
在while当中使用if条件
#!/usr/bin/env python#coding:utf-8print "Please input your age: ",age = int(raw_input())while True: if age >= 60: print "Hi,you shoudn't work !" break else: print "Sorry,please continue work !" break
执行结果:
[root@python ~]# python whitest1.py Please input your age: 60Hi,you shoudn't work ![root@python ~]# python whitest1.py Please input your age: 40Sorry,please continue work !
但是,上面的方法并不怎么好,因为可读性不怎好!
关于while循环我也不知道该说些什么,反正就是提需求上代码 !
玩一个小游戏:
需求,随机产生一个数和自己输入一个数做比较,如果输入的数字和随机的数字相等,那么提示“Congratulations !The number is equal !”,如果输入的数字比随机产生的数字大 ,那么输出:”Sorry, you age wrong,the number is more than random number !”,如果输入的数字比随机产生的数字小,那么输出:“Sorry,you are wrong,The number is less than random number !”
话不多说,上代码 :
#!/usr/bin/env python#coding:utf-8import randomprint "Please input number: ",num = int(raw_input())i = 0while i <= 10: print "************random games****************" randnum = random.randint(1,100) x = 9 - i if num == randnum: print "Congratulations ! the number %s is equal random %s" %(num,randnum) break elif num >= randnum: print "Sorry ,you are wrong ,the number: %s is more than random: %s" %(num,randnum) break elif num <= randnum: print "Sorry,you are wrong ,the number: %s is less than random: %s" %(num,randnum) break print "***********end games********************" i += 1
执行结果:
[root@python ~]# python game.py Please input number: 10************random games****************Sorry,you are wrong ,the number: 10 is less than random: 47[root@python ~]# python game.py Please input number: 53 ************random games****************Sorry ,you are wrong ,the number: 53 is more than random: 6[root@python ~]# python game.py Please input number: 76************random games****************Sorry ,you are wrong ,the number: 76 is more than random: 62[root@python ~]#
发现这个游戏难度太大了,因为随机数是只有在条件为真的时候才产生的,所以,输入不知道多少次才能够成功,所以,现在来改一下代码,让随机数在在条件判断钱就产生,这样玩游戏就很明了。
#!/usr/bin/env python#coding:utf-8import randomrandnum = random.randint(1,100)print "The random number is: %d " % randnumprint "Please input number: ",num = int(raw_input())i = 0while i <= 10: print "************random games****************" x = 10 - i if num == randnum: print "Congratulations ! the number %s is equal random %s" %(num,randnum) break elif num >= randnum: print "Sorry ,you are wrong ,the number: %s is more than random: %s" %(num,randnum) break elif num <= randnum: print "Sorry,you are wrong ,the number: %s is less than random: %s" %(num,randnum) break print "***********end games********************" i += 1
执行结果:
[root@python ~]# python game.py The random number is: 13 Please input number: 13************random games****************Congratulations ! the number 13 is equal random 13[root@python ~]# python game.py The random number is: 74 Please input number: 89************random games****************Sorry ,you are wrong ,the number: 89 is more than random: 74[root@python ~]# python game.py The random number is: 19 Please input number: 10************random games****************Sorry,you are wrong ,the number: 10 is less than random: 19[root@python ~]#
这时候又有一个新的需求了,用户在任何时候输入数字的时候是不可靠的,那么就需要检测用户输入数字的有效性,如果用户输入的数字有效,那么用户输入的数字是否是在1–100之间呢?
我们需要对上述代码进行一个小小的修改。
#!/usr/bin/env python#coding:utf-8import randomrandnum = random.randint(1,100)print "The random number is: %d " % randnumwhile True: print "************random games****************" num_input = raw_input("Please input a interger number in 1 to 100: ") if not num_input.isdigit(): print "Please input a interger number: " elif int(num_input) < 0 or int(num_input) >= 100: print "The input number should be in 1 to 100" else: if randnum == int(num_input): print "Congratulations ! the number %s is equal random %s" %(randnum,num_input) break elif randnum >= int(num_input): print "Sorry ,you are wrong ,the number: %s is more than random: %s" %(randnum,num_input) break elif randnum <= int(num_input): print "Sorry,you are wrong ,the number: %s is less than random: %s" %(randnum,num_input) break else: print "You should be a machine ! bye bye ."~ ~
说明:代码中对于判断是否为数字,使用num.isdigit( ) 函数
执行结果说明:
[root@python ~]# python game.py The random number is: 78 ************random games****************Please input a interger number in 1 to 100: 78Congratulations ! the number 78 is equal random 78[root@python ~]# python game.py The random number is: 73 ************random games****************Please input a interger number in 1 to 100: 43Sorry ,you are wrong ,the number: 73 is more than random: 4[root@python ~]# python game.py The random number is: 38 ************random games****************Please input a interger number in 1 to 100: 96Sorry,you are wrong ,the number: 38 is less than random: 96[root@python ~]# python game.py The random number is: 95 ************random games****************Please input a interger number in 1 to 100: string #输入一个字符串Please input a interger number: ************random games****************Please input a interger number in 1 to 100: . #输入一个半角句号Please input a interger number: ************random games****************Please input a interger number in 1 to 100: / #输入一个斜杠Please input a interger number: ************random games****************Please input a interger number in 1 to 100: 343 #数字不在1到100之间The input number should be in 1 to 100************random games****************Please input a interger number in 1 to 100: 543The input number should be in 1 to 100************random games****************Please input a interger number in 1 to 100: 12Sorry ,you are wrong ,the number: 95 is more than random: 1[root@python ~]# vi game.py [root@python ~]#
while中的break和continue关键字
break和continue关键字经常出现杂循环当中,
break使用来当循环条件满足,执行语句块后马上跳出循环体,执行循环体外的语句。
continue则是从当前位置跳到循环体的最后一行的后面,但是不执行循环体内的最后一行,对于一个循环体来讲,最后一行的后面就是开始,继续执行一个循环体。
break
#!/usr/bin/env python#coding:utf-8num = 4while num: if num % 4 == 0: break print "No exec !" else: print "%d is jishu" %num num = 0print "The number is %d " %num
执行结果:
[root@python ~]# python bretest.py
The number is 4
[root@python ~]#
说明break后面的print 语句并没有执行
continue
#!/usr/bin/env python#coding:utf-8num = 9while num: if num % 4 == 0: num -= 1 continue #如果是偶数,就重新循环,直到条件为假 else: print "the later number is %d " %num #如果是奇数,那么输出奇数 num -= 1
执行结果:
[root@python ~]# python contest.py
the later number is 9
the later number is 7
the later number is 6
the later number is 5
the later number is 3
the later number is 2
the later number is 1
while循环到这就结束了,接下来的学习就是要进行文件的学习了,文件的学习是非常重要的,尤其是那些喜欢学习网络爬虫的同学,文件的学习是必不可少的,因此,这一部分需要认真学习。明天更新,补昨晚没有更新的东西。
完成于2017 05 12
凌晨00:24
- Python学习之While循环
- Python学习笔记之while循环
- Python之While循环
- Python学习笔记(While循环)
- python学习总结---while循环
- python学习之语句:循环语句(for,while)
- java学习之while循环
- C#学习之While循环
- Python新手学习基础之循环语句——While循环
- python 学习(五) While 循环语句
- Python学习笔记-循环for&while
- Python学习---用户输入和while循环
- Python学习(9)--for和while循环
- Python之if,for和while循环
- Python基础篇之while循环
- python学习之while语句
- Python入门学习之While
- java学习之do...while循环
- Lab 3
- 【HTML】碎片知识点-02
- 专业人士对指针的精解
- 数列的逆序数对(难度系数:2颗星)
- 我所理解的MVC架构
- Python学习之While循环
- 创建临时表空间组+查询临时表空间组+临时表空间移动到别的临时表空间组中
- C输入流scanf()
- CSS编码意见
- 有关static和作用域的一个小问题,看代码
- studio中导入第三方so库或者Jar包的两种方法
- 网页都涉及哪些常见的增删改查
- 哈夫曼树的操作-05-树9 Huffman Codes
- #本科毕业论文#Python#Ruby#爬虫#Excel