一、while语句
# while示例1:
[root@saltstack python]# cat while1.py
#!/usr/bin/env python
b = 10
running = True
print 'b = %d' % b
while running:
a = int(raw_input('Enter number a: '))
if a == b:
print '\033[32mGood, a is %s equal b is %s !\033[0m' % (a,b)
running = False
elif a < b:
print 'No, it is a little b'
else:
print 'No, it is a greater b'
else:
print 'The loop is done!'
# 执行结果:
[root@saltstack python]# python while1.py
b = 10
Enter number a: 9
No, it is a little b
Enter number a: 20
No, it is a greater b
Enter number a: 10
Good, a is 10 equal b is 10 !
The loop is done!
二、for循环语句
# for语句示例1:
[root@saltstack python]# cat for1.py
#!/usr/bin/env python
for i in range(1,10):
print '\033[36mThe number is %s\033[0m' % i
else:
print 'The loop is done!'
[root@saltstack python]# python for1.py
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The loop is done!
三、break语句
# break示例1:
[root@saltstack python]# cat break1.py
#!/usr/bin/env python
while True:
A = raw_input('Enter number A is: ')
if A == '24':
print "The number is %s !" % A
break
else:
print "You Number is %s, Please continue enter A number." % A
print 'Done!'
# 执行:
[root@saltstack python]# python break1.py
Enter number A is: 22
You Number is 22, Please continue enter A number.
Enter number A is: 33
You Number is 33, Please continue enter A number.
Enter number A is: 88
You Number is 88, Please continue enter A number.
Enter number A is: 24
The number is 24 !
Done!
四、continue语句
# continue示例1:
[root@saltstack python]# cat continue1.py
#!/usr/bin/env python
while True:
A = int(raw_input('Enter a number A: '))
if A == 24:
print "The number is %s !" % A
break
if A > 20:
continue
print "Please continue enter number."
print 'Done.'
# 执行结果:
[root@saltstack python]# python continue1.py
Enter a number A: 10
Please continue enter number.
Enter a number A: 9
Please continue enter number.
Enter a number A: 33
Enter a number A: 44
Enter a number A: 24
The number is 24 !
Done.
转载于:https://blog.51cto.com/7424593/1839698