Day04 流程控制 while 和for循环
python中使用缩进来区分代码块的
语法 一:
#python
if 条件:
代码块1
代码块2
自上而下依次运行
语法二:
# python
if 条件一:
代码一
else:
代码二 练习:
sex = "female"
age = 18
is_beatuiful = True
is_successful = True
height = 1.7 if sex == 'female ' and age >16 and age < 24 and is_beatuiful and height >1.60 and height <1.8:
print("开始表白")
if is_successful:
print("在一起…………")
else:
print("什么狗屁爱情")
print('阿姨好')
else:
print("深夜里独自买醉")
语法三:
#python
if 条件1:
if 条件2:
代码1
代码2
代码3
语法四:
#python
if 条件一:
elif 条件二:
else: 练习:
学生输入成绩,对学生成绩进行评价 [90-100] 为优秀,[80,90)为良好,
[70,80)为普通,低于七十分为很差。
grades = int(input("请输入你的成绩>>>"))
if grades >= 90:
print('优秀')
elif grades >= 80:
print('良好')
elif grades >= 70:
print('普通')
else:
print('很差')
千万不要在while循环中写带有运算类的代码,不然会容易产生死循环
while 条件:
code_block1
code_block1
code_block1
#python
while True:
name = input('plz input your name')
pwd = input ('plz input you pwd')
if name == 'egon' and pwd =='123'
print('Log in success ')
else:
print('Username or password is incorrect')
结束循环的方式
# python
方式一:条件改为False,
在条件改为False时不会立即结束掉循环,而是要等到下一次循环判断条件时才会生效 tag=True
while tag:
name=input('please input your name: ')
pwd=input('please input your password: ') if name == 'egon' and pwd == '123':
print('login successful')
tag=False
else:
print('username or password error') print('===>')
#python
方式二:while+break
break一定要放在循环体内,一旦循环体执行到break就会立即结束本层循环 while True:
name=input('please input your name: ')
pwd=input('please input your password: ') if name == 'egon' and pwd == '123':
print('login successful')
break
else:
print('username or password error') print('===>>>>>')
print('===>>>>>')
while+continue:结束本次循环,直接进入下一次循环 # 示例一
count=1
while count < 6: #count=6
if count == 4:
count += 1
continue print(count)
count+=1 # 实例二
while True:
name=input('please input your name: ')
pwd=input('please input your password: ') if name == 'egon' and pwd == '123':
print('login successful')
break
else:
print('username or password error')
# continue # 此处加continue无用
了解知识
hile + else: while 条件:
代码1
代码2
代码3
else:
在循环结束后,并且在循环没有被break打断过的情况下,才会执行else的代码 tag=True
while tag:
print(1)
print(2)
print(3)
# tag=False
break
else:
print('else的代码')
while 条件1:
while 条件2:
代码1
代码2
代码3
示范一:
while True:
name=input('please input your name: ')
pwd=input('please input your password: ')
if name == 'egon' and pwd == '123':
print('login successful')
while True:
print("""
0 退出
1 取款
2 转账
3 查询
""")
choice=input('请输入您要执行的操作:') #choice='1'
if choice == '0':
break
elif choice == '1':
print('取款。。。')
elif choice == '2':
print('转账。。。')
elif choice == '3':
print('查询')
else:
print('输入指令错误,请重新输入')
break
else:
print('username or password error')
# 示范二:
tag=True
while tag:
name=input('please input your name: ')
pwd=input('please input your password: ')
if name == 'egon' and pwd == '123':
print('login successful')
while tag:
print("""
0 退出
1 取款
2 转账
3 查询
""")
choice=input('请输入您要执行的操作:') #choice='1'
if choice == '0':
tag=False
elif choice == '1':
print('取款。。。')
elif choice == '2':
print('转账。。。')
elif choice == '3':
print('查询')
else:
print('输入指令错误,请重新输入')
else:
print('username or password error')
for 迭代器循环
for循环的强大之处在于循环取值
l = ['a','b','c'] for i in l:
print(i)
for +break
break 退出本层循环
nums=[11,22,33,44,55]
for x in nums:
if x == 44:
break
print(x)
for+continue
for+else
for + range
for的嵌套
for i in range(3):
for j in range(4):
print(i,j)
Day04 流程控制 while 和for循环的更多相关文章
- Laravel 5.6 视图用Blade语法传递变量和流程控制if 语句和循环语句
Laravel5.6 视图用Blade语法传递变量和流程控制if 语句和循环语句 Laravel 的 View 部分是内置了两套输出系统:直接输出和使用 Blade 引擎“编译”后输出,默认情况下它们 ...
- Java基础-程序流程控制第二弹(循环结构)
Java基础-程序流程控制第二弹(循环结构) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 流程控制有三种基本结构:顺序结构,选择结构和循环结构.一个脚本就是顺序结构执行的,选择结 ...
- day04流程控制,if分支结构,while,for循环
复习 ''' 1.变量名命名规范 -- 1.只能由数字.字母 及 _ 组成 -- 2.不能以数字开头 -- 3.不能与系统关键字重名 -- 4._开头有特殊含义 -- 5.__开头__结尾的变量,魔法 ...
- day04流程控制之while循环
流程控制之while循环 1.什么是while循环 循环指的是一个重复做某件事的过程 2.为何有循环 为了让计算机能像人一样重复 做某件事 3.如何用循环 ''' # while循环的语法:while ...
- 黑马程序员——C语言基础 流程控制 选择结构和循环结构
---恢复内容开始--- Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)流程控制 1> 顺序结构:默认的流程 ...
- day04 流程控制
在python中流程控制主要有三种:顺序流程.分支流程.循环流程 1.顺序流程:在宏观上,python程序的运行就是自上而下的顺序流程: 2.分支流程:分支流程主要是 if...else....流程 ...
- day_4流程控制之分支结构循环结构及for循环
复习一下昨天的内容 1:变量的命名规范 只能由数字 字母 及下划线组成 不能以数字开头 不能与系统关键字重名 _开头有特殊含义 __开头__结尾的变量是魔法变量 支持大小驼峰 ,但建议使用下划线连接语 ...
- java基础 流程控制和条件语句,循环语句
顺序结构 程序的顺序结构: 如果代码里没有流程控制,程序是按照书写的格式从上而下一行一行执行的, 一条语句执行完之后继续执行下一条语句,中间没有判断和跳转,直到程序的结束. if语句 if语句使用bo ...
- Fortran流程控制与逻辑运算、循环--xdd
1.IF语句 1 if() then ... end if 2 if() then ... else ... end if 3 if() then ... else if() then ... els ...
随机推荐
- ROS Learning-025 (提高篇-003 A Mobile Base-01) 控制移动平台
ROS 提高篇 A Mobile Base-01 - 控制移动平台 - 基本知识 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 ...
- QT中显示图像数据
博客转载自:https://blog.csdn.net/lg1259156776/article/details/52325091 一般图像数据都是以RGBRGBRGB……字节流的方式(解码完成后的原 ...
- (转)HTML&CSS——background: url() no-repeat 0 -64px;CSS中背景图片定位方法
http://blog.csdn.net/oscar92420aaa/article/details/51304067 CSS中背景图片的定位,困扰我很久了.今天总算搞懂了,一定要记下来. 在CSS中 ...
- 发现C#winform编程中不常用的控件(一)<FlowLayoutPanel控件><拆分器控件Splitcontainer >
第一部分:FlowLayoutPanel控件 实现效果: 将FlowLayoutPanel做为导航菜单按钮的容器 以实现 某个菜单按钮不显示时 整体的导航菜单布局不至于"缺憾" 原 ...
- Linux操作系统下IPTables配置方法详解
如果你的IPTABLES基础知识还不了解,建议先去看看. 们来配置一个filter表的防火墙 1.查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables -L -n Cha ...
- Java 深入变量和封装思想小结
1.变量的分类和初始值 成员变量:有初始值 局部变量:没有初始值 2.类字段 static :存在于方法区里面 实例变量(实例字段):存在于堆里面 局部变量:存在于栈里面 方法的覆盖: 子类覆盖父类: ...
- 从一个xaml文件获取xaml内容,遍历寻找对象
- HeadFirst设计模式中的笔记
1.『策略模式』 定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. OO基础:抽象 封装 多态 继承 OO原则:封装变化 多用组合,小用继承 针对接口编 ...
- symbol访问法及symbor注册表
symbol主要作用是防止对象属性名冲突 在ES6之前,对象的属性名只能是字符串,这样很容易造成字符串的冲突. 例:假设person对象是从外部库引入的一个对象 let person = { name ...
- File类和Directory类
File类和Directory类分别用来对文件和各种目录进行操作,这两类可以被实例化,但不能被其他类集成. 1. File类(静态类) File类支持对文件的基本操作,它包括用于创建.复制.删除.移动 ...