in
not in
>= < <= == != # 返回一个布尔值
if 条件:
代码1
代码2
if 条件:
代码1
代码2
else:
代码1
if ....else 表示if成立代码会干嘛,else不成立会干什么
if 条件1:
代码1
代码2
....
elif 条件2:
....
elif 条件3:
....
else:
代码
if...elif...else表示if条件1成立干什么,elif条件2成立干什么,elif条件3成立干什么,elif...否则干什么
while True:
print('1')
print('2')
break
print('3')
1
2
break--终止当前的循环,执行其他代码
count =0
while n < 6:
if n==5:
continue
print(n)
n +=1
continue----终止本次的循环,进行下次循环
# tag控制循环退出
tag = True
while tag:
user_db = 'jsom'
pwd_db = '155345'
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
while tag:
cmd = input('请输入你需要的命令:')
if cmd == 'q':
tag = False
print(f'{cmd} 功能执行')
else:
print('username or password error')
print('退出了while循环')
username: jsom
password: 155345
login successful
请输入你需要的命令:q
q 功能执行
退出了while循环
n =1
while n < 3:
print(n)
n +=1
else:
print('没有被break掉就执行')
1
2
没有被break掉就执行
else--会在while没有被break时,才会执行else中的代码
原文:https://www.cnblogs.com/shaozheng/p/11506631.html