7、条件语句与循环语句
今天我们看看条件语句与循环语句。
预习:
1、使用while循环输出1 2 3 4 5 6 8 9 10
2、求1-100的所有数的和
3、输出 1-100 内的所有奇数
4、输出 1-100 内的所有偶数
5、求1-2+3-4+5 ... 99的所有数的和
6、用户登陆(三次机会重试)
本篇导航:
当我们写的程序需要分支时,也可以说当一个事件发生时在特定情况下会产生不同处理情况时就会用到我们的条件语句。
if...else语句:
单分支:
1 ''' 2 if 条件 : 3 满足条件后执行的代码 4 ''' 5 6 age = 18 7 if age == 18 : 8 print("我成年了!")
双分支:
1 ''' 2 if 条件 : 3 满足条件后执行的代码 4 else 5 不满足if时执行 6 ''' 7 8 age = 19 9 if age <= 18 : 10 print("我未年!") 11 else : 12 print("我成年了!")
多分支:
1 ''' 2 if 条件 : 3 满足条件后执行的代码 4 elif 条件 : 5 不满足上面条件执行 6 elif 条件 : 7 不满足上面条件执行 8 ... 9 else 10 不满足上面条件执行 11 ''' 12 13 age = 19 14 if age <= 18 : 15 print("我还未年!") 16 elif age >= 18 : 17 print("我已经成年了!") 18 else : 19 print("我今年刚成年!")
缩进:
在其它的语言里,大多通过{}来确定代码块,而python里没有{}这就是python中的一大特色。那python怎么确定执行的代码块呢?这就引出了一个概念强制缩进,目的是为了让程序知道,每段代码依赖哪个条件,如果不通过缩进来区分,程序无法确定执行的代码块。
Python的缩进原则:
顶级代码必须顶行写,即如果一行代码本身不依赖于任何条件,那它必须不能进行任何缩进
同一级别的代码,缩进必须一致
官方建议缩进用4个空格,当然你也可以按你习惯的方式缩进。
while语句:
1 ''' 2 while 条件 : 3 满足条件后执行的代码 4 ''' 5 6 count = 0 7 while count <= 100 : #只要count<=100就不断执行下面的代码 8 print("loop ", count ) 9 count +=1 #每执行一次,就把count+1,要不然就变成死循环啦,因为count一直是0
while...else语句:
与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句。while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句。
死循环:
有一种循环叫死循环,一但进入死循环,程序就会运行到天荒地老永远无法退出。
while 是只要后边条件永远成立(也就是条件结果永远为真)就一直执行。
例如:上面的代码,如果没有代码 count += 1 ,程序将进入死循环。因为count = 0 ,count <= 100 永远成立。
循环终止语句:
如果在循环的过程中,因为某些原因,你不想继续循环了,就要用到break 或 continue 终止语句。
break:完全跳出循环,执行循环后的代码。
continue:跳出本次循环,不执行continue后的代码,重新进入循环进行循环的条件判断。
for循环:
不依赖索引的取值
1 for i in range (4) : # i 为变量 (4)取值范围 2 print(">>:",i) # 0 1 2 3 3 4 for i in range (1,5) : # 顾头不顾尾 5 print(">>:",i) # 1 2 3 4 6 7 for i in range (1,5,2) : # 步长2 每两个取一个值 8 print(">>:",i) # 1 3
range:顾头不顾尾,默认从0开始
for_else的使用规则和while_else相同。
九九乘法表练习:
1 for i in range(1,10) : 2 for j in range(1,i+1) : 3 print("%s*%s=%s" %(j,i,i*j),end=" ") 4 print()
结果:
预习解答:
1 #使用while循环输出1 2 3 4 5 6 8 9 10 2 count = 1 3 while count <= 10 : 4 print(count) 5 count += 1 6 if count == 7 : 7 count += 1 8 9 #count = 0 10 #while count < 10 : 11 # count += 1 12 # if count == 7 : 13 # continue 14 # print(count)
1 #求1-100的所有数的和 2 count = 1 3 sum = 0 4 while count <= 100 : 5 sum += count 6 count += 1 7 print(sum)
1 #输出 1-100 内的所有奇数 2 count = 1 3 while count <= 100 : 4 print(count) 5 count += 2
1 #输出 1-100 内的所有偶数 2 count = 2 3 while count <= 100 : 4 print(count) 5 count += 2
1 #求1-2+3-4+5 ... 99的所有数的和 2 count = 1 3 sum = 0 4 while count < 100 : 5 if count % 2 == 1 : 6 sum += count 7 else : 8 sum -= count 9 count += 1 10 print(sum)
1 #用户登陆(三次机会重试) 2 username = "oldbody" 3 password = 10086 4 count = 1 5 print("请输入账户密码共三次尝试机会!") 6 while count <= 3 : 7 name = input("请输入账户:") 8 pswd = int(input("请输入密码:")) 9 if name == username and pswd == password : 10 print("输入正确!") 11 break 12 else : 13 print("第",count,"输入错误请重新输入!") 14 count += 1
课后练习:
1.编写for循环,利用索引遍历出每一个字符(选做题)msg='hello egon 666' 2.编写while循环,利用索引遍历出每一个字符msg='hello egon 666' 3.msg='hello alex'中的alex替换成SB 4.msg='/etc/a.txt|365|get'将该字符的文件名,文件大小,操作方法切割出来 5.编写while循环,要求用户输入命令,如果命令为空,则继续输入 6.编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入 7.编写while循环,让用户输入内容,判断输入的内容以alex开头的,则将该字符串加上_SB结尾 8. 1.两层while循环,外层的while循环,让用户输入用户名、密码、工作了几个月、每月的工资(整数),用户名或密码为空,或者工作的月数不为整数,或者 月工资不为整数,则重新输入 2.认证成功,进入下一层while循环,打印命令提示,有查询总工资,查询用户身份(如果用户名为alex则打印super user,如果用户名为yuanhao或者wupeiqi 则打印normal user,其余情况均打印unkown user),退出功能 3.要求用户输入退出,则退出所有循环(使用tag的方式) 运行效果如下: user: egon password: 123 work_mons: 12 salary: 10000 1 查询总工资 2 查询用户身份 3 退出登录 >>: 1 总工资是: 120000.0 1 查询总工资 2 查询用户身份 3 退出登录 >>: 2 unkown user 1 查询总工资 2 查询用户身份 3 退出登录 >>: 3
答案:
#1:编写for循环,利用索引遍历出每一个字符(选做题)msg='hello egon 666' msg='hello egon 666' for i in msg : print(i) #2:编写while循环,利用索引遍历出每一个字符 msg='hello egon 666' msg = 'hello egon 666' max = len(msg) count = 0 while count < max : print(msg[count]) count += 1 #3:msg='hello alex'中的alex替换成SB msg = 'hello alex' print(msg.replace('alex','SB')) #4:msg='/etc/a.txt|365|get'将该字符的文件名,文件大小,操作方法切割出来 msg = '/ect/a.txt|365|get' msg_n = msg.split('/',2)[2] msg_n = msg_n.split('|') print(msg_n[0]) print(msg_n[1]) print(msg_n[2]) #5.编写while循环,要求用户输入命令,如果命令为空,则继续输入 while True : cmd = input(">>>").strip() print("==>",cmd) if len(cmd) ==0 : continue break #6.编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入 while True : name = input("请输入用户名:") password = input("请输入密码:") if len(name) == 0 or name.isdigit() : print("用户名格式有误!") continue break #7.编写while循环,让用户输入内容,判断输入的内容以alex开头的,则将该字符串加上_SB结尾 while True : cmd = input("请输入内容:") if cmd.startswith("alex") : print(cmd+"SB") break #8. tag = True msg ='''1 查询总工资 2 查询用户身份 3 退出登录''' while tag : user = input("user:") .strip() password = input("password:").strip() work_mons = input("work_mons:").strip() salary = input("salary:").strip() if len(user) == 0 or len(password) == 0 or (not work_mons.isdigit()) or (not salary.isdigit()) : print("输入格式有误!") continue while tag : print(msg) cmd = int(input(">>:").strip()) if cmd == 1 : print("总工资是:",(float(salary)*float(work_mons))) continue if cmd == 2 : if user == "alex" : print("super user") elif user == "wupeiqi" or user == "yuanhao": print("normal user") else: print("unkown user") continue if cmd == 3 : tag = False
小知识点:
print()自带一个换行符。
如果想取消默认换行符加end(""),详情可以参考九九乘法表的代码。