第一章 循环语句

控制语句 if...else

if 条件:

    代码

else:

    print(‘ ’)

if ...else...

if 条件:

    代码

elif 条件:

    代码

elif 条件:

    代码

else:

    代码

While 循环

count = 0

while count<=100:

    print("loop",count)

    count+=1

死循环

count=0

while True:

    print('aisfioasudfiaus',count)

    count+=1

循环终止语句

break

count=0

while count<100:

    print('loop', count)

    if count ==5:

    break

    count+=1

print('-----out of while loop ------')

continue

count=0

while count <=100:

count+=1

    if count>5 and count <95:

       continue

    print("loop", count)

print("----out of while loop ----")

while ... else ...

count=0

while <=5:

    count+=1

    print("loop",count)

else:

    print('')

print("-----------")

作业:

猜年龄

age_of_oldboy=8

age=input('请输入年龄>>: ')

if age==age_of_oldboy:

    print('正确')

    break

else:

    print('错误,请重新输入)

age=input('请输入年龄>>: ')

if age==age_of_oldboy:

    print('正确')

    break

else:

    print('错误,请重新输入)

age=input('请输入年龄>>: ')

if age==age_of_oldboy:

    print('正确')

    break

else:

    print('错误超过三次,退出)

count=0
age_of_oldboy=8

while count<3:
age = int(input('请输入年龄>>: '))
if age == age_of_oldboy:
print('正确')
break
else:
print('错误')
count += 1

if count==3:
agent=input('错误次数超过三次,继续按y,否则按n')
if agent== 'y'or agent == 'Y':
count=0
continue
elif agent == 'N' or agent == 'n':
break



登陆接口
用户3次认证失败后,退出程序,再次启动程序尝试登陆时,还是锁定状态(用户锁定状态存在文件里)
dic={'user1':{'psw':'123','count':0},
'user2':{'psw':'123','count':0},
'user3':{'psw':'123','count':0},
}
while True:
name=input('请输入用户名>: ')
if not name in dic:
print('请重试')
continue
with open('user.txt','r') as read_f:
bb=read_f.read().splitlines()
if name in bb:
print('账户已锁定')
continue


psw=input('请输入密码>: ')
if dic[name]['count'] >2:
print('锁定')
with open('user.txt', 'a+') as write_f:
write_f.write(name)
write_f.write('\n')
break


if psw ==dic[name]['psw']:
print('登陆成功')
break
else:
print('请重试')
dic[name]['count']+=1
continue


猜你喜欢

转载自www.cnblogs.com/m-cai/p/8921397.html