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 ...
随机推荐
- aspose ppt转图片
如果直接转图片,会很模糊采用先将ppt转pdf,在通过pdf转图片,这样出来的结果就非常清晰 var pptFileName = "公司网络及计算机使用与要求.pptx"; Pre ...
- Linux 查看设置系统语言
一.简介 在Linux的系统中经常碰到字符集导致的错误,本文总结了设置修改系统语言环境的方法步骤. 二.操作步骤 执行如下指令,查看当前使用的系统语言 echo $LANG 执行如下指令,查看系统安装 ...
- Git 之 初使用
什么是Git? Git 是一个开源的分布式版本控制软件,用以有效.高速的处理从很小到非常大的项目版本管理. Git 最初是由Linus Torvalds设计开发的,用于管理Linux内核开发.Git ...
- hadoop运行常见问题FAQ
问题1:Shuffle Error: Exceeded MAX_FAILED_UNIQUE_FETCHES; bailing-outAnswer:程序里面需要打开多个文件,进行分析,系统一般默认数量是 ...
- HttpServletResponse和HttpServletRequest详解.RP
HttpServletResponse,HttpServletRequest详解 1.相关的接口 HttpServletRequest HttpServletRequest接口最常用的方法就是获得 ...
- Python--socket和threading编程
网络编程基础(一) TCP/IP协议 OSI/RM协议 特点:独立于计算机硬件和操作系统,统一分配网络地址,提供可靠服务,隔离了物理网络的硬件差异 协议分层(百度):网络接口层:IEE802.3(以太 ...
- 自动化打包资源混淆集成python实践----打包
1.自动化打包方案 1)友盟多渠道多渠道打包 2)gradle productFlavors系统的条件编译 3)美团打包 4)APK文件注释写入渠道号 2.各打包方案简介 1)友盟多渠道多渠道打包(w ...
- How a web page loads
The major web browsers load web pages in basically the same way. This process is known as parsing an ...
- Linux中的Makefile
在Linux中Makefile扮演一个非常重要的角色,我们可以以Linux为平台在上面编写我们需要的C程序代码, 对于C语言来说,Linux是一个非常好的平台来学习.使用.调试.验证C代码的平台,其强 ...
- [C# 线程处理系列]专题四:线程同步
目录: 一.线程同步概述 二.线程同步的使用 三 .总结 一.线程同步概述 前面的文章都是讲创建多线程来实现让我们能够更好的响应应用程序,然而当我们创建了多个线程时,就存在多个线程同时访问一个共享的资 ...