python学习日记10——循环

1.while

while 条件判断:
    执行体

2.for-in

#字符串
for i in 'python':
    print(i)           #输出  p,y,t,h,o,n
#序列
for i in range(10):
    print(i)           #输出  0,1,2,3,4,5,6,7,8,9
#不需要用到变量
for _ in range(5):
    print('hello')     #输出5遍 hello

3.break

跳出循环

4.continue

结束本次循环,进入下次循环

5.for-else,while-else

#for循环正常进行完执行else,否则(如有break)不执行else
for i in range(3):
    执行体1
else:
    执行体2
#while-else同理
while 条件:
    执行体1
else:
    执行体2

举例:输入密码888,有三次机会,超过三次输错输出 ‘对不起,您的机会用完了’

for i in range(3):
    s = input('请输入密码:')
    if(s == '888'):
        print('输入正确')
        break
    else:
        print('输入错误')
else:
    print('对不起,您的机会用完了')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值