一、流程控制之判断
if:主要用于判断事物的对错,真假,是否可行
语法结构: if 条件: 代码块1 elif 条件: 代码块2 else 条件: 代码块3 例1: gender = "female" age = 18 is_beautiful = True is_success = True if age == 18 and gender == 'female' and is_beautiful == True: print("表白小姐姐") if is_success: print('在一起') else: print('什么爱情不爱情的,爱nmlgb的爱情') else: print('走开') 例2: score = input('>>>:') score = int(score) if 90 <= score: print("优秀") elif score >= 80: print("良好") elif score >= 70: print("中等") elif score >= 60: print("及格") else: print("不及格") 例3:#登录功能 username = input('请输入您的用户名:').strip() password = input('请输入您的密码:').strip() if username == "bob" and password == '123': print("登录成功") else: print("登录失败")
二、流程控制之循环
while循环:
语法结构: while 条件: 代码块1 代码2 代码3 ...... # while的运行步骤: # 步骤1:如果条件为真,那么依次执行:代码1、代码2、代码3、...... # 步骤2:执行完毕后再次判断条件,如果条件为True则再次执行:代码1、代码2、代码3、......,如果条件为False,则循环终止 continue:跳过本次循环,执行下一次循环 ***** continue下面不管有多少行代码,都不会执行 注意:在循环中,如果使用continue这个关键字,在使用关键字之前,需要确认循环的计数是否修改,否则可能会导致死循环 break:结束本层循环,单纯指代当前while ***** 只能结束一层循环
Python中的计数方法:
几乎所有的程序语言都选择从0开始计数,因此,除非需求的特殊要求,否则循环的计数都从0开始
死循环 count = 0 while True: print(count) count += 1 #打印50遍hello word #1.定义一个整数变量,记录循环次数 i = 1 #2.开始循环 while i <= 50 #1,希望在循环内执行的代码块 print("hello world") #2,处理计数器 i = i + 1 用户认证程序: # 限制登录次数,如果登录3次失败,锁定账户 username = "bob" password = "123" #记录错误验证的次数 count = 0 while count < 3: inp_name = input('请输入您的用户名:') inp_psw = input('请输入您的密码:') if inp_name == username and inp_psw == password: print("登录成功") break # 结束本层循环,后面的不执行了 else: print("登录失败") count += 1 if count == 3 print('锁定账户') 打印1-10之间除7以外的数字 count = 0 while count < 10: count += 1 if count == 7: continue print(count)
while + else的使用:
当while 循环正常执行完并且中间没有被break 中止的话,就会执行else后面的语句
例: count =0 while count < 5: count += 1 print(count) else: print("循环结束") #没有被break打断,所以执行了该行代码 如果执行过程中被break,就不会执行else后的语句 例: count =0 while count < 5: count += 1 if count == 3: break print(count) else: print("循环结束") #由于循环被break打断了,所以不执行else后的输出语句
while嵌套:
username = "bob" password = "123" count = 0 while count < 3: #第一层循环 inp_name = input('请输入您的用户名:') inp_psw = input('请输入您的密码:') if inp_name == username and inp_psw == password: print("登录成功") while True: #第二次循环 cmd = input(">>>:") if cmd == "exit": break print("执行指令cmd") break # 结束本层循环,后面的不执行了 else: print("登录失败") count += 1 if count == 3: print('锁定账户')
#改进 username = "bob" password = "123" tag = True count = 0 while tag: #第一层循环 inp_name = input('请输入您的用户名:') inp_psw = input('请输入您的密码:') if inp_name == username and inp_psw == password: print("登录成功") while tag: #第二次循环 cmd = input(">>>:") if cmd == "exit": tag = False else: print("执行指令cmd") else: print("登录失败") count += 1 if count == 3: print('锁定账户')
## for循环:
for:给我们提供了一种不依赖于索引的取值方式
语法结构: for 变量 in 容器类型(列表,字符串,字典): 容器对象中有几个值,他就循环几次 #字典对象,无法直接访问值value
例1 for item in "abc": print(item) #运行结果: a b c 例2: for item in ["bob","jack","mary"]: print(item) #运行结果: bob jack mary 例3 d1 = {'a':1,'b':2,"c":3} for i in d1: print(d1[i]) #运行结果 1 2 3 注意:range() 顾头不顾尾 # start stop sep for i in range(0,10,9): print(i) #运行结果 0 9 例5: for i in range(0,10) print(i)
注意:break 与 continue也可以用于for循环,使用语法同while循环
for i in range(0,10): if i == 7: continue if i == 9: break print(i)
for+else:
for循环正常执行结束,就会执行else对应的代码块
非正常结束,例如break打断,就不会执行
for i in range(10): if i == 5: break print(i) else: print('执行成功')
for嵌套:
#请用for循环嵌套的方式打印如下图形: ***** ***** ***** for i in range(3): for j in range(5): print("*",end='') print() # print()表示换行
#打印九九乘法表 """ 九九乘法表: 1x1=1 2x1=2 2x2 =4 。。。。 """ 方法一: row=1 while row<=9: col =1 while col<=row: print("%d * %d = %d" %(col,row,col*row),end="\t") col +=1 # print("第 %s 行 " % row) print() row +=1 方法二: for i in range(1,10): for j in range (1,i+1): print(f'{i}x{j}={i*j}',end="\t") print()
例: """打印: * *号:1 空格:4 *** *号:3 空格:3 ***** *号:5 空格:2 ******* *号:7 空格:1 ********* *号:9 空格:0 """ max_level = 5 for i in range(1,max_level+1): for j in range(max_level-i): print(" ",end="") for z in range(2*i-1): print("*",end="") print()
来源:https://www.cnblogs.com/baohanblog/p/12143024.html