python基础学习-for循环

#打印0-100的偶数

for i in range(0,101,2):#0表示开头,101表示结尾,2表示步长

    print(i)

#登录三次验证

#方法一:

_user="abc"

_password="123"

passed_authentication=False#表示flag(标志位)

for i in range(3):

    username=input("username:")

    password=input("password")

    if username==_user and password==_password:

        print("welcome")

        passed_authentication=True

        break#表示中断循环

    else:

        print("账号或者密码错误")

if not passed_authentication:

    print("试了三次了,别试了")

#方法二:

_user="abc"

_password="123"

for i in range(3):

    username=input("username:")

    password=input("password")

    if username==_user and password==_password:

        print("welcome")

        break#表示中断循环

    else:

        print("账号或者密码错误")

else:

    print("试了三次了,别试了")

小结:

1、方法二,for......else......表示只要for循环正常执行完毕(被break中断也是正常执行完毕),那么就不会执行else语句。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值