04 Python入门学习-流程控制(if else elif while for)
一:流程控制if
语法一:
if 条件:
code1
code2
code3
...
age = 20
height = 170
weight = 60
sex = 'female'
is_beautiful = True if age> 12 and age<25 and weight == 60 and height >168 and sex == 'female' and is_beautiful:
print('这就是我的晓晖女神')
语法二
if 条件:
code1
code2
code3
...
else:
code1
code2
code3
...
age = 20
height = 170
weight = 60
sex = 'female'
is_beautiful = True if age> 12 and age<25 and weight == 60 and height >168 and sex == 'female' and is_beautiful:
print('这就是我的晓晖女神')
else:
print('走开走开!')
语法三:多分支
强调:if的多分支 = 但凡有一个条件成立.就会往下再判断其他条件了
if 条件1:
code1
code2
code3
...
elif :
code1
code2
code3
...
elif:
code1
code2
code3
...
else:
code1
code2
code3
...
小练习,判断成绩的等级
score=int(input('please input your score'))
if score>=90:
print('优秀')
elif score>=80:
print('良好')
elif score >=70:
print('普通')
else:
print('小伙子,你的成绩很差啊')
语法四:if 的嵌套
print('女神'.center(50,'*'))
name = 'xiaohui'
age = 18
height = 165
weight = 60
sex = 'female'
is_beautiful = True
if name == 'xiaohui' and age > 14 and age < 20 and height > 160 and sex == 'female':
print('心动的感觉')
if is_beautiful:
print('大声说,我喜欢你')
else:
print('逗你玩的呢')
else:
print('打扰了??????')
如果:今天是Monday,那么:上班
如果:今天是Tuesday,那么:上班
如果:今天是Wednesday,那么:上班
如果:今天是Thursday,那么:上班
如果:今天是Friday,那么:上班
如果:今天是Saturday,那么:出去浪
如果:今天是Sunday,那么:出去浪# today=input('>>: ')
if today == 'Monday':
print('上班')
elif today == 'Tuesday':
print('上班')
elif today == 'Wednesday':
print('上班')
elif today == 'Thursday':
print('上班')
elif today == 'Friday':
print('上班')
elif today == 'Saturday':
print('出去浪')
elif today == 'Sunday':
print('出去浪')
else:
print('''必须输入其中一种:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
''')
today=input('>>: ')
if today == 'Monday' or today == 'Tuesday' or today == 'Wednesday' or today == 'Thursday' or today == 'Friday':
print('上班')
elif today == 'Saturday' or today == 'Sunday':
print('出去浪')
else:
print('''必须输入其中一种:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
''')
二: 流程控制while
1 :什么是循环
循环就是一个重复的过程
2 为什么要有循环
我们想要让程序像人一样重复的去做某一件事情
3 怎么用循环
while 条件
code 1
code 2
code 3
...
username = 'andy'
password = ''
while Turn:
inp_name = input('please input your username')
inp_pwd = input('please input your password')
if inp_name == username and inp_pwd == password:
print('恭喜你,登陆成功')
else :
print('用户名或密码错误')
while+break 方法
在while循环中,break代表结束本层循环
代码实例:
username = 'andy'
password = ''
while Turn:
inp_name = input('please input your username')
inp_pwd = input('please input your password')
if inp_name == username and inp_pwd == password:
print('恭喜你,登陆成功')
break
else :
print('用户名或密码错误')
while+continue 方法
在while循环中,continue代表结束本次循环
代码实例
#删除掉1-100中所有包含4的数
#并且将剩下的数存到一个列表中
li=[]
for i in range(1,101):
if i[-1]==4:
continue
li.append(i) print(li)
while的嵌套循环:
username = 'andy'
password = ''
while Turn:
inp_name = input('please input your username')
inp_pwd = input('please input your password')
if inp_name == username and inp_pwd == password:
print('恭喜你,登陆成功')
print('下面是一个小程序')
while True:
inp= input('你叫什么名字?')
quest = inp + ',你好.我是人工智障'
agein_inp = input('输入q退出,输入其他开启智障模式')
if agein_inp=='q':
break
print(quest)
else :
print('用户名或密码错误')
while + else
else的代码会在while循环没有break打断的情况下最后运行
n=1 while n < 5: if n == 4: break print(n) n+=1 else: print('=====》')
上面的程序代码会被break打断,所以不会执行下面的else语句
三:流程控制for
for循环:可以不依赖索引而取值
#for 循环对象为列表 items = ['武大','武二','张三','李四','王五','赵六']
for item in items:
print(item) #for 循环对象为字典 dict = {'name':'andy','age':18,'height':180}
for dic in dict:
print(dic)
for vs while
for可以不依赖索引取值,是一种通用的循环取值方式
for循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的
nums = [1,2,3,4,5,6,2,5,6,3,6,9,9,5,4,3,5,3,6,3,]
for i in range(0,len(nums)):
print(nums[i])
04 Python入门学习-流程控制(if else elif while for)的更多相关文章
- 04-Python入门学习-流程控制
一.流程控制if 语法1: if 条件: code1 code2 code3 .... age=180 height=163 weight=75 sex='female' is_beautif ...
- Python入门6 —— 流程控制 - if判断
代码块: 1.代码块指的是同一级别的代码,在python中用缩进相同的空格数(除了顶级代码块无任何缩进之外,其余代码块都是在原有的基础上缩进4个空格)来标识同一级的代码块 2.同一级别的代码块会按照自 ...
- python入门之流程控制
if else 格式: if 条件 command1 command2elif 条件: command3 command4 else: command3 command4 注意条件后和else后 ...
- python语法入门之流程控制
python语法入门之流程控制 流程控制是指控制流程,具体指控制程序执行的流程. 流程控制分为三种: 1.顺序结构 程序从上而下的正常执行(正常执行的代码就是顺序结构) 2.分支结构 赋予程序人的思维 ...
- python入门学习:6.用户输入和while循环
python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...
- Python3.7.4入门-2流程控制工具
2 流程控制工具 记得在语句后加冒号 2.1 while # Fibonacci series: # the sum of two elements defines the next a, b = 0 ...
- Python 入门之流程控制语句
Python 入门之流程控制语句 1.if判断 (1) 单 if if –-如果 if 条件: 缩进 结果 (官方推荐4个空格,或者一个tab 不能空格和tab混合使用) money = 10 pri ...
- python中的流程控制
目录 引言 流程控制的分类 分支结构 单if结构 if与else结构 if与elif与else结构 if分支的嵌套 循环结构 while循环 while + break循环 while + conti ...
- python入门学习:9.文件和异常
python入门学习:9.文件和异常 关键点:文件.异常 9.1 从文件中读取数据9.2 写入文件9.3 异常9.4 存储数据 9.1 从文件中读取数据 9.1.1 读取整个文件 首先创建一个pi_ ...
随机推荐
- Html页面Dom对象之Event
HTML DOM Event 对象 实例 哪个鼠标按钮被点击? 光标的坐标是? 被按的按键的 unicode 是? 相对于屏幕,光标的坐标是? shift 键被按了吗? 哪个元素被点击了? 哪个事件类 ...
- 限制mongodb内存占用过高方法
1.mongodb必须是以服务的方式启动的.即能用service mongodb start的方式启动 资源限制用这个命令systemctl set-property <servicename& ...
- LoadRunner安装+破解+汉化
安装 一.需要准备的东东: 1.电脑的操作系统:Win7旗舰版(不解释,这个版本安装问题最少了) 2.LoadRunner11+破解文件+汉化文件+删除注册表工具 3.强大的搜索引擎 二.安装过程 用 ...
- Spring Boot 报错:Error creating bean with name 'entityManagerFactory' defined in class path resource
spring boot 写一个web项目,在使用spring-data-jpa的时候,启动报如下错误: Error starting ApplicationContext. To display th ...
- Tomcat服务器学习和使用(二)
一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命令的用法如下:
- linux 线程的同步 三 (内存信号量的使用)
信号量.同步这些名词在进程间通信时就已经说过,在这里它们的意思是相同的,只不过是同步的对象不同而已.但是下面介绍的信号量的接口是用于线程的信号量,注意不要跟用于进程间通信的信号量混淆,关于用于进程间通 ...
- 汇编_指令_FLAGS
标志名 标志 1 标志 0 OF (溢出标志) OV ...
- 租用游艇(简单区间dp)
租用游艇 时间限制: 1 Sec 内存限制: 128 MB提交: 1 解决: 1[提交][状态][讨论版][命题人:quanxing] 题目描述 长江游艇俱乐部在长江上设置了n 个游艇出租站1,2 ...
- 积木城堡(dp)
题目描述 XC的儿子小XC最喜欢玩的游戏用积木垒漂亮的城堡.城堡是用一些立方体的积木垒成的,城堡的每一层是一块积木.小XC是一个比他爸爸XC还聪明的孩子,他发现垒城堡的时候,如果下面的积木比上面的积木 ...
- win2008以上的系统,在vmware esxi5.5里怎么使用自定义规范管理器?sysprep
经过测试,原来08以上的系统自带了sysprep.exe,所以vcenter对08以上的系统直接使用自定义规范管理器即可,跟linux一样了.注意不要跟03一样写入了sn即可. vCenter可使用s ...