if语句:
1 if 条件1: 2 pass 3 elif 条件2: 4 pass 5 elif 条件3: 6 pass 7 else: 8 pass
if 条件语句中,先判断条件1,如果满足条件1,则执行第二行代码,第二行执行完后if条件语句结束。
示例:用户取款机菜单用户选择界面:
from time import sleep num = input("Please input your chioce:") if num == "1": print("取款中.....") sleep(3) print("取款完成!") elif num == "2": print("存款中....") sleep(3) print("存款完成!") elif num == "3": print("查询中....") sleep(3) print("查询完成!") elif num == "4": print("退出中....") sleep(1) print("已退出!") else: print("Please input the right nummber!")
——————————————————————————————————————————
while循环 :while 条件: 用于循环执行某段代码
示例:用户登陆界面
while True: name = input("Please input your name :") pwd = input("Please input your password :") if name == "egon" and pwd == "123": print("login successful") else: print("name or password Error! Please input again!")
while循环可以嵌套while循环
示例:用户登陆退出
name = "egon" password = "123" tag = True while tag: User_name = input("Name >>>") User_pwd = input("Password >>>") if User_name.lower() == name and User_pwd == password: while tag: Request = input("Request >>>") if Request.lower() == "quit": print("Over") tag = False else: print("Name or Password Error,Please input again")
while和break,continue搭配使用
示例1:while和break搭配 用户登陆退出
name = "egon"
password = "123"
while True: User_name = input("Name >>>") User_pwd = input("Password >>>") print(User_name.lower()) if name.lower() == User_name and password == User_pwd: print("login successful!") request = input("Request >>>") while True: if request.lower() =="quit": print("Over!") break break else: print("Name or Password Error,Please ipnut again!")
循环中遇到break即退出当前循环执行循环外代码。
示例2:while和continue搭配使用,筛选输出值
while True: val = input("请输入数据以逗号分隔:") val = val.split(",") try: val = list(map(int,val)) except ValueError as e: continue count = 1 while count < 20: if count in val: count += 1 continue else: print(count) count += 1
continue的功能是当执行到continue时不再执行continue以后子代码块语句直接回到循环判断,进行下一次循环
当val = list(map(int,val))语句出错时执行continue语句,当执行到continue语句时立马重新回到当前循环判断条件。
——————————————————————————————————————————
for循环:for循环可以实现的功能while循环统统可以实现,但在某些场景,for循环更简洁,比如取值
如:
L = [1,2,3,4] for i in L: print(i) >>> 1 2 3 4
for 嵌套
示例1:打印99乘法表
L = [] for i in range(1, 10): for j in range(1, i + 1): m = "%s * %s = %s" % (i, j, i*j) L.append(m) print(*L) L = []
>>>>
C:\Users\DELL\AppData\Local\Programs\Python\Python37\python3.exe C:/Users/DELL/PycharmProjects/untitled1/ad.py
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
示例2:金字塔
分析: * #第一行4个空格,1个* * * * #第二行3个空格,3个* * * * * * #第三行2个空格,5个* * * * * * * * # .... * * * * * * * * * max_level = 5 L = [] for current_level in range(1 , max_level+1): # print(current_level) for m in range(max_level-current_level): L.append(" ")#将空格装入列表 for n in range(2*current_level-1): L.append("*")#将*装入列表 print(*L) L = [] #里层for循环完一次重新定义L为空列表,准备装下一次的循环的元素
——————————————————————————————————————————
while,for,break等综合应用
from time import sleep name = "egon" password = "123" while True: User_name = input("Please input your ID:") User_pwd = input("Please input your Password:") if User_name == name and User_pwd == password: print("login successful") print(""" 1 取款 2 存款 3 查询 4 退出 """) while True: num = input("Please input your chioce:") if num == "1": print("取款中.....") sleep(3) print("取款完成!") elif num == "2": print("存款中....") sleep(3) print("存款完成!") elif num == "3": print("查询中....") sleep(3) print("查询完成!") elif num == "4": print("退出中....") sleep(1) print("已退出!") break else: print("Please input the right nummber!") break
>>>>>
C:\Users\DELL\AppData\Local\Programs\Python\Python37\python3.exe C:/Users/DELL/PycharmProjects/untitled1/ad.py Please input your ID:egon Please input your Password:123 login successful 1 取款 2 存款 3 查询 4 退出 Please input your chioce:1 取款中..... 取款完成! Please input your chioce:2 存款中.... 存款完成! Please input your chioce:3 查询中.... 查询完成! Please input your chioce:4 退出中.... 已退出! Process finished with exit code 0