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('对不起,您的机会用完了')