python

break和continue
练习:

#! usr/bin/python
for i in range(0, 10, 2):
        for j in range(0, 10, 2):
                if j == 4:
                        print ('**j =',j)
                        continue
                print ('*j =',j)
        print ('i=',i)

结果:

[root@node1]/py# python test001.py
*j = 0
*j = 2
**j = 4
*j = 6
*j = 8
i= 0
*j = 0
*j = 2
**j = 4
*j = 6
*j = 8
i= 2
*j = 0
*j = 2
**j = 4
*j = 6
*j = 8
i= 4
*j = 0
*j = 2
**j = 4
*j = 6
*j = 8
i= 6
*j = 0
*j = 2
**j = 4
*j = 6
*j = 8
i= 8

练习2:

while 1:
        letter=input("请输入3种字母a,e,r:")
        if letter=='e':
                break
        elif letter=='a':
                continue
        elif letter=='r':
                letter=input("请重新输入一遍:")
                print ("letter:",letter)

练习:

n=3
while n>0:
        letter=input('请输入3中字母a,e,r:')
        n-=1
        if letter=='e':
                break
        elif letter=='a':
                continue
        elif letter=='r':
                letter=input("请重新输入一个:")
                print ("letter:",letter)

结果:

请输入3种字母a,e,r:a
请输入3种字母a,e,r:r
请重新输入一遍:r
letter: r
请输入3种字母a,e,r:e

你可能感兴趣的:(Python)