python 基础,,if 判断[root
python 基础,,if 判断[root
if 判断
[root@es-3 ~]# cat test.py #-*-encoding:utf-8 -*-‘‘‘if 1 < 2 :print("hajjj")if False :print(3456)print(787)if 1 < 3 :print(123456)else:print(56789)num = input(‘请输入您猜的数字‘)if num == ‘1‘:print("看《活着》")elif num == ‘2‘:print("看《摆渡人》")elif num == ‘3‘:print("看《岛上书店》")else:print("学python")score = int(input("输入分数: "))if score > 100:print("厉害,牛")elif score >= 90:print("A")elif score >= 70:print("B")elif score >= 60:print("C")else:print("笨了")‘‘‘name = input(‘请输入名字:‘)age = input(‘请输入年龄‘)if name == ‘小二‘:if age == ‘18‘:print(‘666‘)else:print(333)测试[root@es-3 ~]# python3 test.py 请输入名字:小san请输入年龄18[root@es-3 ~]# python3 test.py 请输入名字:小二请输入年龄18666[root@es-3 ~]# cat test.py #-*-encoding:utf-8 -*-‘‘‘if 1 < 2 :print("hajjj")if False :print(3456)print(787)if 1 < 3 :print(123456)else:print(56789)num = input(‘请输入您猜的数字‘)if num == ‘1‘:print("看《活着》")elif num == ‘2‘:print("看《摆渡人》")elif num == ‘3‘:print("看《岛上书店》")else:print("学python")score = int(input("输入分数: "))if score > 100:print("厉害,牛")elif score >= 90:print("A")elif score >= 70:print("B")elif score >= 60:print("C")else:print("笨了")‘‘‘name = input(‘请输入名字:‘) age = input(‘请输入年龄‘)if name == ‘小二‘: name等于小二是就进入下一个判断;否则直接退出if age == ‘18‘: 判断age是否等于18,如果等于18就打印666print(‘666‘)else:print(333) 否则打印333测试[root@es-3 ~]# python3 test.py 请输入名字:小san请输入年龄18[root@es-3 ~]# python3 test.py 请输入名字:小二请输入年龄18666[root@es-3 ~]# python3 test.py 请输入名字:小二 请输入年龄12333
循环
无限死循环
[root@es-3 ~]# vim test.py print(‘9090‘)while True: print(‘chenxi‘) print(‘流浪‘) print(‘痒‘)print(‘1010‘)[root@es-3 ~]# python3 test.py9090chenxi流浪痒chenxi流浪痒chenxi流浪痒chenxi流浪^Z[1]+ 已停止 python3 test.py
循环语句终止,打印1到10
[root@es-3 ~]# cat test.py count = 1flag = Truewhile flag:print(count)count = count + 1if count > 10 :flag = False[root@es-3 ~]# python3 test.py 12345678910
循环语句终止,另一种写法
[root@es-3 ~]# vim test.py count = 1while count <= 10: print(count) count = count + 1[root@es-3 ~]# python3 test.py 12345678910
计算从1加到100
[root@es-3 ~]# vim test.py count = 1sum = 0while count <= 100: sum = sum + count count = count + 1print(sum)[root@es-3 ~]# python3 test.py 5050
循环语句终止之二break;跳本次循环
[root@es-3 ~]# cat test.py print(‘11‘)while True:print(‘222‘)print(‘333‘)breakprint(‘444‘)print(‘abc‘)[root@es-3 ~]# python3 test.py 跳出循环没有44411222333abccontinue和break有点类似,区别在于continue只是终止本次循环,接着还执行后面的循环,break则完全终止循环。
[root@es-3 ~]# cat test.py count = 0while count <= 100:count += 1if count > 5 and count < 95:continue 本次循环结束print("loop",count)print("----out----")[root@es-3 ~]# python3 test.py loop 1loop 2loop 3loop 4loop 5loop 95loop 96loop 97loop 98loop 99loop 100loop 101----out----
python 基础
评论关闭