您的位置:首页 > 编程语言 > Python开发

学习python笔记——控制流

2012-11-10 10:34 375 查看
简介

在到目前为止我们所见到的程序中,总是有一系列的语句,Python忠实地按照它们的顺序执行它们。如果你想要改变语句流的执行顺序,该怎么办呢?例如,你想要让程序做一些决定,根据不同的情况做不同的事情,例如根据时间打印“早上好”或者“晚上好”。

你可能已经猜到了,这是通过控制流语句实现的。在Python中有三种控制流语句——if、for和while。

1. 选择结构

      用if-elif-else实现:

if i < 1:
print("less than 1")
elif i < 3:
print("less than 3")
else:
print("else")
print("Done")

 

例子:if.py

#!/usr/bin/python
# Filename: if.py
number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:

print 'Congratulations, you guessed it.' # New block starts here

print "(but you do not win any prizes!)" # New block ends here
elif guess < number:

print 'No, it is a little higher than that' # Another block

# You can do whatever you want in a block ...
else:

print 'No, it is a little lower than that'

# you must have guess > number to reach here
print 'Done'
# This last statement is always executed, after the if statement is executed

运行结果:

$ python if.py
Enter an integer : 50
No, it is a little lower than that
Done
$ python if.py
Enter an integer : 22
No, it is a little higher than that
Done
$ python if.py
Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done


需要注意的地方:

      (1)正确的缩进格式,否则运行报错

      (2)选择分支后的冒号

2. 循环结构

      循环结构可以用while或者for来实现。

      while循环:

当while循环条件变为False的时候,else块才被执行——这甚至也可能是在条件第一次被检验的时候。如果while循环有一个else从句,它将始终被执行,除非你的while循环将永远循环下去不会结束!

 

running = True
i = 1
while running:
if i == 5:
running = False
i = i + 1
else:
print("while loop done")

例子:while.py

#!/usr/bin/python
# Filename: while.py
number = 23
running = True
while running:

guess = int(raw_input('Enter an integer : '))
if guess == number:

print 'Congratulations, you guessed it.'

running = False # this causes the while loop to stop

elif guess < number:

print 'No, it is a little higher than that'

else:

print 'No, it is a little lower than that'
else:

print 'The while loop is over.'

# Do anything else you want to do here
print 'Done'


运行结果:

$ python while.py
Enter an integer : 50
No, it is a little lower than that.
Enter an integer : 22
No, it is a little higher than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done

需要注意的地方:

      (1)正确的缩进格式,否则运行报错

      (2)while 条件后有:

for循环:

#!/usr/bin/python
# Filename: for.py
for i in range(1, 5):

print i
else:

print 'The for loop is over'


输出:

输出

$ python for.py
1
2
3
4
The for loop is over

我们所做的只是提供两个数,range返回一个序列的数。这个序列从第一个数开始到第二个数为止。例如,range(1,5)给出序列[1, 2, 3, 4]。默认地,range的步长为1。如果我们为range提供第三个数,那么它将成为步长。例如,range(1,5,2)给出[1,3]。记住,range 向上 延伸到第二个数,即它不包含第二个数。

 

break语句

break语句是用来 终止 循环语句的,即哪怕循环条件没有称为False或序列还没有被完全递归,也停止执行循环语句。

一个重要的注释是,如果你从for或while循环中 终止 ,任何对应的循环else块将不执行。

例子:break.py

#!/usr/bin/python
# Filename: break.py
while True:

s = raw_input('Enter something : ')

if s == 'quit':

break

print 'Length of the string is', len(s)
print 'Done'


输出:

$ python break.py
Enter something : Programming is fun
Length of the string is 18
Enter something : When the work is done
Length of the string is 21
Enter something : if you wanna make your work also fun:
Length of the string is 37
Enter something :    use Python!
Length of the string is 12
Enter something : quit
Done


 

continue语句

continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后 继续 进行下一轮循环。

使用continue语句

例子:continue.py

#!/usr/bin/python
# Filename: continue.py
while True:

s = raw_input('Enter something : ')

if s == 'quit':

break

if len(s) < 3:

continue

print 'Input is of sufficient length'

# Do other kinds of processing here...


结果:

 

$ python continue.py
Enter something : a
Enter something : 12
Enter something : abc
Input is of sufficient length
Enter something : quit


     

 
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: