函数的if--while流程控制
一、流程控制---if
1.if条件判断
age=18
hight=1.70
sex="female"
is_beautiful=True
if sex=="female" and age>16and age<20 and is_beautiful \
and hight>1.60 and hight<1.75:
print("表白。。。。")
2.if+else
hight=1.70
sex="female"
is_beautiful=True
if sex=="female" and age>16and age<20 and is_beautiful \
and hight>1.60 and hight<1.75:
print("表白。。。。")
if is_beautiful:
print("在一起。。。")
else:
print("什么爱情不爱情""和我有半毛钱关系")
3.if的嵌套:
#语法三:if 的嵌套
age=18
hight=1.70
sex="female"
is_beautiful=True
if sex=="female" and age>16and age<20 and is_beautiful \
and hight>1.60 and hight<1.75:
print("表白。。。。")
if is_beautiful:
print("在一起。。。")
else:
print("什么爱情不爱情""和我有半毛钱关系")
else:
print("阿姨好。。。")
print(" code ...")
4.if-elif的运用:
如果 成绩>=90,那么:优秀
如果 成绩>=80且<90,那么:良好
如果 成绩>=70且<80,那么:普通
其他情况:很差
"""score=input("输入成绩:")
score=int(scar)
if scar>=90:
print("优秀")
elif score>=80:
print("良好")
elif score>=70:
print("普通")
else:
print("极差")"""
二、流程控制--while
1.while循环又称为条件循环
while条件循环
"""
while True:
name=input("please input yourname:")
password=input("please input your passwor;")
if name=="adb" and password=="123":
print("login successful")
else:
print("username or password error")
2.while+条件结束
tag=True
while tag:
name=input("please input yourname:")
password=input("please input your passwor;")
if name=="adb" and password=="123":
print("login successful")
tag=False
else:
print("username or password error")
3.while+break:退出本层循环
while True:
name = input("please input yourname:")
password = input("please input your passwor;")
if name == "adb" and password == "123":
print("login successful")
break
4.while+continue:终止本次循环,进行下次循环,跳过了本次循环之后的代码
ount=0
while count<6:
if count==4:
count+=1
continue
print(count)
count+=1
5.while+else:当while循环正常执行完,中间没有break中止的话,执行else
# ---while-else----
count = 1
while count < 6:
if count == 4:
count += 1
continue
print(count)
count += 1
else:
print("while-else最后else的代码")
三、流程控制--for
for循环又称为迭代循环
for可以不依赖于索引取指,是一种通用的循环取指方式
循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的
for+break
for+continute
for+else
和while一样的用法
1.0 range() 》》》通常用于取值不(依赖索引)
range(1,5) >>>1 2 3 4#g顾头不顾尾, 所以只能取到 range(5) >>>1 2 3 4 range(1,5,2) #补偿 ,每隔两个取值#i = i + 2 >>>1 3
2.0嵌套循环:
for i in range(1,10):
for j in range(1,i+1): print("%s*%s=%s" %(i,j,i*j,end="") print()#换行
本章练习:
.练习,要求如下:
1.1用户登录:
name=input("please your name:")
password=input("please your password:")
if name =="egon" and password=="123456":
print("login successful....")
break
else:
print("name or password error....")
1.2管理员登录系统:
egon --> 超级管理员
tom --> 普通管理员
jack,rain --> 业务主管
其他 --> 普通用户
ame=input("请输入名字:")
if name=="egon":
print("超级管理员")
elif name=="tom":
print("普管理员")
elif name=="jick,rain":
print("业务主管")
else:
print("普通用户")
1.3
如果:今天是Monday,那么:上班
# 如果:今天是Tuesday,那么:上班
# 如果:今天是Wednesday,那么:上班
# 如果:今天是Thursday,那么:上班
# 如果:今天是Friday,那么:上班
# 如果:今天是Saturday,那么:出去浪
# 如果:今天是Sunday,那么:出去浪
"""
"""
today_is=input("今天是礼拜几:")
if today_is=="monday" or today_is=="tuestday" or today_is=="wedneesday"or \
today_is=="thursday"or today_is=="friday":
print("working...")
elif today_is=="sunday" or today_is=="saturday":
print("playing...")
else:
print("必须输入:"
"monday"
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday" )
2.1.while循环练习题
1 循环验证用户输入的用户名与密码
2 认证通过后,运行用户重复执行命令
3 当用户输入命令为时,则退出整个程序
name = "egon"
password = "123456"
count = 0
while True:
inp_name = input("please your name:")
inp_password = input("please your password:")
if name == inp_name and password == inp_password:
print("login successful....")
print("""
1 取钱
2 存款
3 退出
""")
while True:
choice = input("please input your choice")
if choice == "1":
print("取钱")
elif choice == "2":
print("存钱")
elif choice == "3":
break
else:
print("你的输入有误,请重新输入")
else:
print("name or password error....")
count += 1
if count == 3:
break
2.1 迭代式循环:for for in range():作业九九乘法表
for i in range(1,10): for j in range(1,i+1): print("%s*%s=%s" %(i,j,i*j,end="") print()
1*1=1 2*1=22*2=4 3*1=33*2=63*3=9 4*1=44*2=84*3=124*4=16 5*1=55*2=105*3=155*4=205*5=25 6*1=66*2=126*3=186*4=246*5=306*6=36 7*1=77*2=147*3=217*4=287*5=357*6=427*7=49 8*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=64 9*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81
2.2 制作金字塔
感谢您的阅览,有不足之处还请之处,共同学习! 内容持续更新中。。。
函数的if--while流程控制的更多相关文章
- 「C」 函数、运算、流程控制
一.函数 (一)什么是函数 任何一个C语言程序都是由一个或者多个程序段(小程序)构成的,每个程序段都有自己的功能,我们一般称这些程序段为“函数”. (二)函数的定义 目的:将一个常用的功能封装起来,方 ...
- Postman高级应用——流程控制、调试、公共函数、外部数据文件
postman客户端下载地址:https://www.getpostman.com/apps 目录 流程控制 调试 公共函数 外部数据文件 流程控制 流程控制简言之就是设置接口的执行顺序,流程控制只有 ...
- MySQL-5-TCL,视图,变量,存储过程和函数,流程控制
TCL:Transaction Control Language事务控制语言 TCL 事务的特点 acid: 原子性(Atomicity),一致性(Consistency),隔离性(isolation ...
- EventProxy流程控制
EventProxy流程控制 EventProxy是一个通过控制事件触发顺序来控制业务流程的工具. 1. 利用事件机制解耦复杂业务逻辑2. 移除被广为诟病的深度callback嵌套问题3. 将串行等待 ...
- Scala深入浅出实战经典-----002Scala函数定义、流程控制、异常处理入门实战
002-Scala函数定义.流程控制.异常处理入门实战 Scala函数定义 语句结束无分号 定义无参函数 def 函数名称(参数名称:参数类型)[:Unit=]{ 函数体 } 老师的代码 我的实际代码 ...
- javascript之流程控制 和函数的容易忽略点
1.流程控制 1> for in 仅用于 对象的遍历: var box={ "name":'小红', 'age':18, 'height':165 }; for(var b ...
- awk(流程控制、内置变量、内置函数、数组)
摘自:http://bbs.51cto.com/thread-883948-1-1.html awk(流程控制.内置变量.内置函数.数组) ... 参考其他的资料,给大家看看.一.awk流程控制语句 ...
- Go 从入门到精通(三)字符串,时间,流程控制,函数
一.strings和strconv的使用 strings strings.HasPrefix(s string,preffix string) bool:判断字符串s是否以prefix开头 stirn ...
- MySQL自定义函数用法详解-复合结构自定义变量/流程控制
自定义函数 (user-defined function UDF)就是用一个象ABS() 或 CONCAT()这样的固有(内建)函数一样作用的新函数去扩展MySQL. 所以UDF是对MySQL功能的一 ...
- JAVA之旅(二)——if,switch,for,while,do while,语句嵌套,流程控制break , continue ,函数,重载的示例总结
JAVA之旅(二)--if,switch,for,while,do while,语句嵌套,流程控制break , continue ,函数,重载的示例总结 JAVA的思想真的很重要,所以要专心的学-- ...
随机推荐
- Hive中知识点
hive的最新学习资料:http://www.cnblogs.com/qingyunzong/p/8707885.html hive的参数设置大全:https://cwiki.apache.org/c ...
- Elastic Stack-Elasticsearch介绍
一.前言 前篇写了好像没有多少人去看,但是还是要继续,我猜想可能是很多人接触的这块比较少吧,Elasticsearch这块有很多要说的,开始吧. 二.数据库.Elasticsearch选择 ...
- 监听 在xshell中
- 创建SVN源库钩子
在源库的hooks目录下面添加post-commit.bat文件,每次代码该文件会自动执行以保证同步到备份服务器 set SVN_HOME="D:\Program Files\VisualS ...
- JS实现刷新页面后回到记录时滚动条的位置
window.onbeforeunload = function () { var scrollPos; if (typeof window.pageYOffset != 'undefined') { ...
- asp.net core Serilog的使用
先贴上关于使用这个日志组件的一些使用方法,等有时间了在吧官方的文档翻译一下吧,现在真是没时间. Serilog在使用上主要分为两大块: 第一块是主库,包括Serilog以及Serilog.AspNet ...
- centos7之sed和awk常用
sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令 ...
- css3多個佈局
分欄佈局: column-count:分欄數 column-gap:欄間間距 column-rule:欄間線條 記得消除瀏覽器兼容:-moz-和-webkit- 盒佈局: display:box 彈性 ...
- ovs之组网实验
介绍 本示例将创建两个OVS实例和两个主机,其中每个OVS上接入一个主机,OVS实例之间有链路连接,形成一个链状拓扑,如图.在OVS组网完成之后,再通过手动方式添加流表,实现网络通信,从而验证实验可行 ...
- <数据结构基础学习>(四)链表 Part 2
一.使用链表实现栈 增,删,查只对链表头进行操作,时间复杂度都为O(1) 链表头作为栈顶 LinkedListStack<E> implements Stack<E> publ ...