python分支与循环_python 条件分支与循环

一、if判断:

语法一:

if 条件:

# 条件成立时执行的子代码块

代码1

代码2

代码3

示例:

sex='female'

age=18

is_beautiful=True

if sex == 'female' and age > 16 and age < 20 and is_beautiful:

print('开始表白。。。')

print('other code1...')

print('other code2...')

print('other code3...')

示例

语法二:

if 条件:

# 条件成立时执行的子代码块

代码1

代码2

代码3

else:

# 条件不成立时执行的子代码块

代码1

代码2

代码3

sex='female'

age=38

is_beautiful=True

if sex == 'female' and age > 16 and age < 20 and is_beautiful:

print('开始表白。。。')

else:

print('阿姨好。。。')

print('other code1...')

print('other code2...')

print('other code3...')

示例

语法三:

if 条件1:

if 条件2:

代码1

代码2

代码3

sex='female'

age=18

is_beautiful=True

is_successful=True

height=1.70

if sex == 'female' and age > 16 and age < 20 and is_beautiful \

and height > 1.60 and height < 1.80:

print('开始表白。。。')

if is_successful:

print('在一起。。。')

else:

print('什么爱情不爱情的,爱nmlgb的爱情,爱nmlg啊.')

else:

print('阿姨好。。。')

print('other code1...')

print('other code2...')

print('other code3...')

示例

语法四:

if 条件1:

代码1

代码2

代码3

elif 条件2:

代码1

代码2

代码3

elif 条件3:

代码1

代码2

代码3

.......

else:

代码1

代码2

代码3

示例:

如果成绩 >= 90,那么:优秀

如果成绩 >= 80且 < 90, 那么:良好

如果成绩 >= 70且 < 80, 那么:普通

其他情况:很差

'''

score = input('please input your score: ') # score='100'

score = int(score)

if score >= 90:

print('优秀')

elif score >= 80:

print('良好')

elif score >= 70:

print('普通')

else:

print('很差')

示例

二、while循环

语法:

while 条件:

代码1

代码2

代码3

while True:

name=input('please input your name: ')

pwd=input('please input your password: ')

if name == 'egon' and pwd == '':

print('login successful')

else:

print('username or password error')

示例

结束while循环的两种方式

方式一:条件改为False,

在条件改为False时不会立即结束掉循环,而是要等到下一次循环判断条件时才会生效

tag=True

while tag:

name=input('please input your name: ')

pwd=input('please input your password: ')

if name == 'egon' and pwd == '':

print('login successful')

tag=False

else:

print('username or password error')

print('===>')

方式二:while+break

break一定要放在循环体内,一旦循环体执行到break就会立即结束本层循环

while True:

name=input('please input your name: ')

pwd=input('please input your password: ')

if name == 'egon' and pwd == '':

print('login successful')

break

else:

print('username or password error')

print('===>>>>>')

print('===>>>>>')

2.1、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 == '':

print('login successful')

break

else:

print('username or password error')

# continue # 此处加continue无用

2.2、while else

while + else:

while 条件:

代码1

代码2

代码3

else:

在循环结束后,并且在循环没有被break打断过的情况下,才会执行else的代码

tag=True

while tag:

print(1)

print(2)

print(3)

# tag=False

break

else:

print('else的代码')

2.3、while嵌套

#语法

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 == '':

print('login successful')

while True:

print("""

0 退出

1 取款

2 转账

3 查询

""")

choice=input('请输入您要执行的操作:') #choice='1'

if choice == '':

break

elif choice == '':

print('取款。。。')

elif choice == '':

print('转账。。。')

elif choice == '':

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 == '':

print('login successful')

while tag:

print("""

0 退出

1 取款

2 转账

3 查询

""")

choice=input('请输入您要执行的操作:') #choice='1'

if choice == '':

tag=False

elif choice == '':

print('取款。。。')

elif choice == '':

print('转账。。。')

elif choice == '':

print('查询')

else:

print('输入指令错误,请重新输入')

else:

print('username or password error')

示例二

三、for循环

1 迭代式循环:for,语法如下

for i in range(10):

缩进的代码块

2 break与continue(同上)

3 循环嵌套

for i in range(1,10):

for j in range(1,i+1):

print('%s*%s=%s' %(i,j,i*j),end=' ')

print()

九九乘法表

python3&period;4学习笔记&lpar;十&rpar; 常用操作符&comma;条件分支和循环实例

python3.4学习笔记(十) 常用操作符,条件分支和循环实例 #Pyhon常用操作符 c = d = 10 d /= 8 #3.x真正的除法 print(d) #1.25 c //= 8 #用两个 ...

Python - 条件控制、循环语句 - 第十二天

Python 条件控制.循环语句 end 关键字 关键字end可以用于将结果输出到同一行,或者在输出的末尾添加不同的字符,实例如下: Python 条件语句是通过一条或多条语句的执行结果(True 或 ...

Python&lpar;四&rpar; 分支、循环、条件与枚举

一.什么是表达式 表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列 二.表达式的优先级 三.表达式优先级练习 优先级同级 从左往右计算 1 or 2 a ...

初学python&lpar;print使用、条件分支、循环、模块引用&rpar;

import random """ #查看源代码日后爬虫用 import urllib.request # coding=utf-8 url = "http:/ ...

python条件判断与循环

条件判断 1.python缩进规则: 如果if语句判断是True,就把缩进的语句执行了,否则,什么也不做,比如: age=20 if age >= 18: print('your age is' ...

Python条件控制与循环语句

1. 条件控制 # if-elif-else结构 age = 12 if age < 4: price = 0 elif age < 18: price = 5 else: price = ...

Python条件判断和循环&comma;range&lpar;&rpar;函数

条件判断经常使用if语句进行判断,表达方式为:if 条件语句:      :elif:else if...用于执行第一条不满足if的判断,继续执行其它的判断.比如一个简单的if判断 Python3取消 ...

【03】Python:分支和循环

写在前面的话 在写代码的时候,我们的代码不可能总是一行一行语句的堆叠,有些时候我们需要对一个事物进行判断,或者很大一组数据需要循环挨个处理.这些不可能让我们一步一步的去写.所以有了接下来的分支结构和循 ...

python之分支和循环

Day 1-night 三元操作符 语法:a=x if 条件 else y  即:当条件为True时,a的值赋值为x,否则赋值为y eg:small=x if x

随机推荐

JS 学习笔记--9---变量-作用域-内存相关

JS 中变量和其它语言中变量最大的区别就是,JS 是松散型语言,决定了它只是在某一个特定时间保存某一特定的值的一个名字而已.由于在定义变量的时候不需要显示规定必须保存某种类型的值,故变量的值以及保存的 ...

对于Android Service 生命周期进行全解析

应用程序组件有一个生命周期——一开始Android实例化他们响应意图,直到结束实例被销毁.在这期间,他们有时候处于激活状态,有时候处于非激 活状态:对于活动,对用户有时候可见,有时候不可见.组件生命周 ...

Solr 教程

1.Solr安装 下载jdk-8u111-windows-i586_8.0.1110.14 下载solr-6.3.0.zip 2.配置JAVA_HOME 在"系统变量"中,设置3项 ...

ASP&period;NET没有魔法——ASP&period;NET MVC 模型绑定解析(上篇)

前面文章介绍了ASP.NET MVC中的模型绑定和验证功能,本着ASP.NET MVC没有魔法的精神,本章内容将从代码的角度对ASP.NET MVC如何完成模型的绑定和验证进行分析,已了解其原理. 本 ...

Centos7修改系统时区

timedatectl status Local time: 四 2014-12-25 10:52:10 CST Universal time: 四 2014-12-25 02:52:10 UTC R ...

day 08字符编码 文件处理

字符编码1.软件启动流程(打开notepad++文档)从硬盘将软件加载到内存上加载test.txt到内存中执行notepad++的代码,将test.txt打到屏幕上 python解释器也是一个应用软件 ...

Spring Security 案例实现和执行流程剖析

Spring Security Spring Security 是 Spring 社区的一个顶级项目,也是 Spring Boot 官方推荐使用的安全框架.除了常规的认证(Authentication ...

JS实现页面字体繁简转换

封装的JS代码 // 网页简繁体转换 // 本js用于客户在网站页面选择繁体中文或简体中文显示,默认是正常显示,即简繁体同时显示 // 在用户第一次访问网页时,会自动检测客户端语言进行操作并提示.此功 ...

ubuntu关闭服务需要身份验证

service tomcat stop ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === 需要通过认证才能停止“tom ...

基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip 【备注】 1、该资源内项目代码百分百可运行,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值