C++猿的Python笔记02-控制流

。_饼干妹妹 提交于 2020-02-14 16:51:22

注意冒号  

  if else  

if ... :

缩进

else :

缩进 

if guess == number:
    
print 'Congratulations, you guessed it.' # New block starts here
    running = False
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

 

  switch  

没有switch,用if..: elif ..: else: 代替

 

  while   

while xx:

缩进 

else:

缩进 

while done == False:
    i 
= i+1;
    
print 'frame', i
else:
    
print 'The while loop is over.'

  

  for  

for element in arrays:

缩进

else:

缩进 

for i in range(15):
print i
else:

print 'The for loop is over'  


  break   

终止循环。

注意:如果你从for或while循环中终止 ,任何对应的循环else块将不执行。 

 

  continue   

逃过本次循环后面的语句,进入下一轮循环。

注意:continue语句对于for循环也有效。 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!