Control Statement if...else
if condition:
code
else:
print(‘ ’)
if ...else...
if condition:
code
elif condition:
code
elif condition:
code
else:
code
while loop
count = 0
while count<=100:
print("loop",count)
count+=1
infinite loop
count=0
while True:
print('aisfioasudfiaus',count)
count+=1
loop termination statement
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("-----------")
Operation:
guess age
age_of_oldboy=8
age=input('Please enter age>>: ')
if age==age_of_oldboy:
print('correct')
break
else:
print('Error, please re-enter)
age=input('Please enter age>>: ')
if age==age_of_oldboy:
print('correct')
break
else:
print('Error, please re-enter)
age=input('Please enter age>>: ')
if age==age_of_oldboy:
print('correct')
break
else:
print('Error more than three times, exit)
count=0
age_of_oldboy=8
while count<3:
age = int(input('Please enter age>>: '))
if age == age_of_oldboy:
print('correct')
break
else:
print('error')
count + = 1
if count==3:
agent=input('If the number of errors exceeds three times, continue to press y, otherwise press n')
if agent== 'y' or agent == 'Y':
count=0
continue
elif agent == 'N' or agent == 'n':
break
the login interface
after the user fails to authenticate 3 times, exit the program, and start the program again to try to log in, it is still in the locked state (the user's locked state is stored in the file)
dic={'user1':{'psw':'123','count':0},
'user2':{'psw':'123','count':0},
'user3':{'psw ':'123','count':0},
}
while True:
name=input('Please enter the user name>: ')
if not name in dic:
print('Please try again')
continue
with open('user .txt','r') as read_f:
bb=read_f.read().splitlines()
if name in bb:
print('Account is locked')
continue
psw=input('Please enter password>: ')
if dic [name]['count'] >2:
print('lock')
with open('user.txt', 'a+') as write_f:
write_f.write(name)
write_f.write('\n')
break
if psw ==dic[name]['psw']:
print('Login successful')
break
else:
print('Please try again')
dic[name]['count']+=1
continue