python 条件分支与循环
2024-08-22 23:21:49
一、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()
九九乘法表
最新文章
- centos 6.7安装与配置vncserver
- CentOS 6忘记密码解决办法,root和普通用户均可
- SAP 创建物料主数据分类视图特性
- AngularJs $cacheFactory 缓存服务
- js返回上一页报网页过期问题解决
- SQL大数据操作统计
- <;input>; 标签
- A*算法深入
- RHEL4-Partition Image系统备份(软件版)
- HDU 3788 和九度OJ 1006测试数据是不一样的
- 作业02-Java基本语法与类库
- Lesson 29 Taxi!
- [Swift]LeetCode402. 移掉K位数字 | Remove K Digits
- JDK安装路径下的JRE与独立安装的JRE区别
- 关于jqGrid组件表格无法自适应宽度问题
- padding内边距
- Unity shader学习之Blinn-Phong光照模型
- Libre 6006 「网络流 24 题」试题库 / Luogu 2763 试题库问题 (网络流,最大流)
- idea创建springcloud项目图文教程(EurekaServer注册中心)
- Ubuntu 13.04 VirtualBox在工作区中的切换