一、流程控制之if...else
1.用户输入得分,判断成绩
如果:成绩>=90,那么:优秀
如果成绩>=80且<90,那么:良好
如果成绩>=70且<80,那么:普通
其他情况:很差
user = int(input("score:")) if score >= 90: print("excellent") elif score >= 80: print("good") elif score >= 70: print("ordinary") else: print("bad")
2.用户登陆
account = "Mouse" pwd = "123" inp_account = input("account:") inp_pwd = input("pwd:") if inp_account == account and inp_pwd == pwd: print("Welcome user %s login..." % inp_account) else: print("account or pwd error")
二、流程控制之while循环
1.while + 条件:
代码块
# 打印0-10 count = 0 while True: if count <= 10: print(count) count += 1
二、break 与continue
1.break:指终止当前层次的循环并执行其他的代码
2.contiue:指终止本次循环,直接进入下一次循环
三、while...else
当while循环正常执行完,中间没有被break中止的话,就会执行后边的文件。
a.有break
count = 0 while count < 10: count += 1 if count == 6: break print(count) else: print("---------") # 1 # 2 # 3 # 4 # 5
b.无break
count = 0 while count < 10: count += 1 if count == 6: continue print(count) else: print("---------") # 1 # 2 # 3 # 4 # 5 # 7 # 8 # 9
A.1.循环验证用户输入的用户名和密码
2.认证通过后,运行用户重复执行命令
3.当用户输入命令为quit时,退出整个程序
name = "Mouse" pwd = "123" while True: inp_name = input("用户名") inp_pwd = input("密码:”) if inp_name == name and inp_pwd == pws: while True: cmd = input(">>:") if not cmd: continue if cmd == "quit" break print("run %s" %cmd) else: print("用户名或者密码错误") continue break
# 使用tag
name = "Mouse" pwd = "123" tag = True while tag: inp_name = input("用户名:") inp_pwd = input("密码:") if inp_name == name and inp_pwd == pwd: while tag: cmd = input(">>:") if not cmd: continue if cmd == "quit": tag = False continue print("run %s" %cmd) else: print("用户名或者密码错误")
三、流程控制之 for 循环
for i in range(10):
代码块