欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python学习之While循环

程序员文章站 2023-11-25 14:06:34
while循环 while循环是在高中学习过的知识,也就是高中所称的当型循环。意思就是当条件满足的时候,就执行循环体内的语句,如果不满足条件,则退出。 需求,如果年龄大于60,...

while循环

while循环是在高中学习过的知识,也就是高中所称的型循环。意思就是当条件满足的时候,就执行循环体内的语句,如果不满足条件,则退出。

需求,如果年龄大于60,那么输出 hi,you shoudn’t work !,否则,输出,”please continue work !

方法一:使用if条件实现

#!/usr/bin/env python
#coding:utf-8

print "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:  60
hi, 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-8

print "please input your age: "

age = int(raw_input())

while age >= 60:
    print "hi, you shoudn\'t work !"
    break
else:
    print "sorry,please continue work !"

执行结果:

[root@python ~]# python whitest.py 
please input your age: 
60
hi, you shoudn't work !
[root@python ~]# python whitest.py 
please input your age: 
40
sorry,please continue work !

上面就是一个while循环,但是,我们再里面增加了关键字break和else才能实现需求,但是我们也可以这么干。
在while当中使用if条件

#!/usr/bin/env python
#coding:utf-8

print "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:  60
hi,you shoudn't work !
[root@python ~]# python whitest1.py 
please input your age:  40
sorry,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-8
import random
print "please input number: ",
num = int(raw_input())
i = 0
while 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-8
import random
randnum = random.randint(1,100)
print "the random number is: %d " % randnum
print "please input number: ",
num = int(raw_input())
i = 0
while 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-8
import random
randnum = random.randint(1,100)
print "the random number is: %d " % randnum
while 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: 78
congratulations ! 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: 43
sorry ,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: 96
sorry,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: 543
the input number should be in 1 to 100
************random games****************
please input a interger number in 1 to 100: 12
sorry ,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-8
num = 4
while num:
    if num % 4 == 0:
        break
        print "no exec !" 
    else:
        print "%d is jishu" %num
        num = 0

print "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-8

num = 9
while 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循环到这就结束了,接下来的学习就是要进行文件的学习了,文件的学习是非常重要的,尤其是那些喜欢学习网络爬虫的同学,文件的学习是必不可少的,因此,这一部分需要认真学习。明天更新,补昨晚没有更新的东西。