首页 > 其他 > 详细

day05-06 continue语句、while循环

时间:2017-11-28 00:33:53      阅读:242      评论:0      收藏:0      [点我收藏+]

登录成功就break,登录不成功就打印

_user = "xiaoyanzi"
_passwd = "woaini"

pass_authentication = False
for i in range(3):
    username = input("username:")
    passwd = input("passwd:")


    if _user == username and _passwd == passwd:
        print("welcome to %s login"%username)
        pass_authentication = True
        break
    else:
        print("invalid username or passwd !!!")

if not pass_authentication:
    print("不洗脚的小燕子!!!")

技术分享图片

可精简为:

_user = "xiaoyanzi"
_passwd = "woaini"


for i in range(3):
    username = input("username:")
    passwd = input("passwd:")


    if _user == username and _passwd == passwd:
        print("welcome to %s login"%username)

        break
    else:
        print("invalid username or passwd !!!")
else:
    print("不洗脚的小燕子!!!")

技术分享图片

while循环(无限循环,又称死循环):

while true:
print("中国人民万岁!")

输出结果为n个:

中国人民万岁!

_user = "xiaoyanzi"
_passwd = "woaini"


while true:  #死循环
    username = input("username:")
    passwd = input("passwd:")


    if _user == username and _passwd == passwd:
        print("welcome to %s login"%username)

        break
    else:
        print("invalid username or passwd !!!")
else:
    print("不洗脚的小燕子!!!")

将死循环改为有限循环:

_user = "xiaoyanzi"
_passwd = "woaini"

counter = 0
while counter < 3:
    username = input("username:")
    passwd = input("passwd:")


    if _user == username and _passwd == passwd:
        print("welcome to %s login"%username)
        
        break
    else:
        print("invalid username or passwd !!!")

    counter += 1


else:
    print("不洗脚的小燕子!!!")

技术分享图片

技术分享图片

三次输入错误之后,是否还想继续3次,再继续3次,效果:

_user = "xiaoyanzi"
_passwd = "woaini"

counter = 0
while counter < 3:
    username = input("username:")
    passwd = input("passwd:")


    if _user == username and _passwd == passwd:
        print("welcome to %s login"%username)
        True
        break
    else:
        print("invalid username or passwd !!!")

    counter += 1
   
     if counter == 3:
        keep_going_choice = input ("还想玩么?[y/n]")
        if keep_going_choice == "y":
            counter = 0
        


else:
    print("不洗脚的小燕子!!!")
    

 

day05-06 continue语句、while循环

原文:http://www.cnblogs.com/minkillmax/p/7906791.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!