标签:
一、条件判断
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
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
循环
##循环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!"
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)
continue
#!/usr/bin/python #coding=utf-8 for i in range(10): if i%2==0: continue print u"这是奇数",i
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
标签:
原文地址:http://www.cnblogs.com/binhy0428/p/5083060.html