码迷,mamicode.com
首页 > 其他好文 > 详细

py流程控制

时间:2015-12-28 16:56:57      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

一、条件判断

if ..else

技术分享
>>> user=yaobin
>>> passwd=yaobin123
>>> user_input=raw_input("you name")
you nameyaobin
>>> passwd_input=raw_input("you password")
you passwordyaobin123
>>> if user_input == user and passwd_input == passwd:
...   print "ok"
... else:
...   print "fail"
... 
ok
View Code

 

elif..else

技术分享
>>> user=yaobin
>>> passwd=yaobin456
>>> user_input=raw_input("you username")
you usernameyaobin
>>> passwd_input=raw_input("you passwd")
you passwdyaobin456
>>> if user_input == user and passwd_input == passwd:
...   print "ok"
... elif user_input == guest:
...   print "ok2"
... else:
...   print "fail!"
... 
ok
View Code

 

循环

##循环3次,判断用户和密码

技术分享
#!/usr/bin/python
#coding=utf-8


vaild_user=yaobin
vaild_passwd=yaobin123


for i in range(3):
    user_input=raw_input("you username")
    passwd_input=raw_input("you passwd")
    if user_input == vaild_user and passwd_input == vaild_passwd:
        print "username and passwd ok!"
        break
    elif  user_input == "guess":
        print "guess access ok!"
        break
    else:
         print "username or passwd error!"
View Code

 

while

技术分享
#!/usr/bin/python
#coding=utf-8

import  time
count = 0
run_forever=True
while run_forever:
   count += 1
   print "Loop",count
   if count == 9:
        run_forever=False
   time.sleep(10)
View Code

 

continue

技术分享
#!/usr/bin/python      
#coding=utf-8          
                       
for i in range(10):    
    if i%2==0:         
        continue       
    print u"这是奇数",i    
View Code

 

break之:子父一起跳出循环

技术分享
#!/usr/bin/python
#coding=utf-8

loop1=1#设定loop1 和loop2 这两个计数器
loop2=1

while True:
    loop1+=1
    print "Loop1:",loop1
    break_flag = False #父标记
    while True:
        loop2 +=1
        if loop2 ==5:
            break_flag=True #让我爹一块往外跳
            break #我先跳出第一层
        print Loop2:,loop2

    if break_flag: #儿子跳了
        print "接到儿子跳出通知,我也得跳了!"
        break
View Code

 

py流程控制

标签:

原文地址:http://www.cnblogs.com/binhy0428/p/5083060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!