day04之流程控制
if语句:
if 条件1:
pass
elif 条件2:
pass
elif 条件3:
pass
else:
pass
if 条件语句中,先判断条件1,如果满足条件1,则执行第二行代码,第二行执行完后if条件语句结束。
示例:用户取款机菜单用户选择界面:
from time import sleep
num = input("Please input your chioce:")
if num == "":
print("取款中.....")
sleep(3)
print("取款完成!")
elif num == "":
print("存款中....")
sleep(3)
print("存款完成!")
elif num == "":
print("查询中....")
sleep(3)
print("查询完成!")
elif num == "":
print("退出中....")
sleep(1)
print("已退出!")
else:
print("Please input the right nummber!")
——————————————————————————————————————————
while循环 :while 条件: 用于循环执行某段代码
示例:用户登陆界面
while True:
name = input("Please input your name :")
pwd = input("Please input your password :")
if name == "egon" and pwd == "":
print("login successful")
else:
print("name or password Error! Please input again!")
while循环可以嵌套while循环
示例:用户登陆退出
name = "egon"
password = ""
tag = True
while tag:
User_name = input("Name >>>")
User_pwd = input("Password >>>")
if User_name.lower() == name and User_pwd == password:
while tag:
Request = input("Request >>>")
if Request.lower() == "quit":
print("Over")
tag = False else:
print("Name or Password Error,Please input again")
while和break,continue搭配使用
示例1:while和break搭配 用户登陆退出
name = "egon"
password = "123"
while True:
User_name = input("Name >>>")
User_pwd = input("Password >>>")
print(User_name.lower())
if name.lower() == User_name and password == User_pwd:
print("login successful!")
request = input("Request >>>")
while True:
if request.lower() =="quit":
print("Over!")
break
break
else:
print("Name or Password Error,Please ipnut again!")
循环中遇到break即退出当前循环执行循环外代码。
示例2:while和continue搭配使用,筛选输出值
while True:
val = input("请输入数据以逗号分隔:")
val = val.split(",")
try:
val = list(map(int,val))
except ValueError as e:
continue count = 1
while count < 20:
if count in val:
count += 1
continue
else:
print(count)
count += 1
continue的功能是当执行到continue时不再执行continue以后子代码块语句直接回到循环判断,进行下一次循环
当val = list(map(int,val))语句出错时执行continue语句,当执行到continue语句时立马重新回到当前循环判断条件。
——————————————————————————————————————————
for循环:for循环可以实现的功能while循环统统可以实现,但在某些场景,for循环更简洁,比如取值
如:
L = [1,2,3,4] for i in L: print(i) >>> 1 2 3 4
for 嵌套
示例1:打印99乘法表
L = []
for i in range(1, 10):
for j in range(1, i + 1):
m = "%s * %s = %s" % (i, j, i*j)
L.append(m)
print(*L)
L = []
>>>>
C:\Users\DELL\AppData\Local\Programs\Python\Python37\python3.exe C:/Users/DELL/PycharmProjects/untitled1/ad.py
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
示例2:金字塔
分析:
* #第一行4个空格,1个*
* * * #第二行3个空格,3个*
* * * * * #第三行2个空格,5个*
* * * * * * * # ....
* * * * * * * * * max_level = 5
L = []
for current_level in range(1 , max_level+1):
# print(current_level)
for m in range(max_level-current_level):
L.append(" ")#将空格装入列表
for n in range(2*current_level-1):
L.append("*")#将*装入列表
print(*L)
L = [] #里层for循环完一次重新定义L为空列表,准备装下一次的循环的元素
——————————————————————————————————————————
while,for,break等综合应用
from time import sleep name = "egon"
password = "" while True:
User_name = input("Please input your ID:")
User_pwd = input("Please input your Password:")
if User_name == name and User_pwd == password:
print("login successful")
print("""
1 取款
2 存款
3 查询
4 退出
""")
while True: num = input("Please input your chioce:")
if num == "":
print("取款中.....")
sleep(3)
print("取款完成!")
elif num == "":
print("存款中....")
sleep(3)
print("存款完成!")
elif num == "":
print("查询中....")
sleep(3)
print("查询完成!")
elif num == "":
print("退出中....")
sleep(1)
print("已退出!")
break
else:
print("Please input the right nummber!")
break
>>>>>
C:\Users\DELL\AppData\Local\Programs\Python\Python37\python3.exe C:/Users/DELL/PycharmProjects/untitled1/ad.py
Please input your ID:egon
Please input your Password:
login successful 1 取款
2 存款
3 查询
4 退出 Please input your chioce:
取款中.....
取款完成!
Please input your chioce:
存款中....
存款完成!
Please input your chioce:
查询中....
查询完成!
Please input your chioce:
退出中....
已退出! Process finished with exit code 0
day04之流程控制的更多相关文章
- day04 运算符 流程控制 (if while/of)
1. 运算符算数运算符 + - * / int / float :数字类型 # print(10 + 3.1)# print(10 / 3)# print(10 // 3)# print(10 % 3 ...
- DAY04、流程控制if、while、for
一.if 判断 语法一: if 条件: # 以下是上一条if 的子代码块 print(子代码1) print(子代码2) print(子代码3) 示例: # 路边飘过一个生物,要不要表白? sex = ...
- day04 流程控制
在python中流程控制主要有三种:顺序流程.分支流程.循环流程 1.顺序流程:在宏观上,python程序的运行就是自上而下的顺序流程: 2.分支流程:分支流程主要是 if...else....流程 ...
- day04流程控制,if分支结构,while,for循环
复习 ''' 1.变量名命名规范 -- 1.只能由数字.字母 及 _ 组成 -- 2.不能以数字开头 -- 3.不能与系统关键字重名 -- 4._开头有特殊含义 -- 5.__开头__结尾的变量,魔法 ...
- day04流程控制之while循环
流程控制之while循环 1.什么是while循环 循环指的是一个重复做某件事的过程 2.为何有循环 为了让计算机能像人一样重复 做某件事 3.如何用循环 ''' # while循环的语法:while ...
- Day04 流程控制 while 和for循环
一.流程控制 if 判断 python中使用缩进来区分代码块的 语法 一: #python if 条件: 代码块1 代码块2 自上而下依次运行 语法二: # python if 条件一: 代码一 el ...
- 第10章 Shell编程(4)_流程控制
5. 流程控制 5.1 if语句 (1)格式: 格式1 格式2 多分支if if [ 条件判断式 ];then #程序 else #程序 fi if [ 条件判断式 ] then #程序 else # ...
- Shell命令和流程控制
Shell命令和流程控制 在shell脚本中可以使用三类命令: 1)Unix 命令: 虽然在shell脚本中可以使用任意的unix命令,但是还是由一些相对更常用的命令.这些命令通常是用来进行文件和文字 ...
- PHP基础知识之流程控制的替代语法
PHP 提供了一些流程控制的替代语法,包括 if,while,for,foreach 和 switch. 替代语法的基本形式是把左花括号({)换成冒号(:),把右花括号(})分别换成 endif;,e ...
随机推荐
- os模块-subprocess 模块- configpaser 模块
一. os 模块 主要用于处理与操作系统相关操作,最常用文件操作 使用场景:当需要操作文件及文件夹(增,删,查,改) os.getcwd() 获取当前工作目录 os.chdir('dirname') ...
- 码云git使用一(上传本地项目到码云git服务器上)
主要讲下如果将项目部署到码云git服务器上,然后使用studio导入git项目,修改本地代码后,并同步到码云git上面. 首先:我们在码云上注册账号并登陆.官网(https://git.oschina ...
- angular 常用插件集合
md5加密 https://www.npmjs.com/package/md5-typescript angular echarts https://github.com/xieziyu/ng ...
- shell script auto generate the relevant header information
first : add follow context in /etc/vim/vimrc set ignorecaseset cursorlineset autoindentautocmd Buf ...
- Linux command automake
Linux command automake [Purpose] Learning linux command automake for generate Makefile.in for ...
- python实现用户登录界面
要求 输入用户名密码正确,提示登录成功, 输入三次密码错误,锁定账户. 实现原理: 创建两个文件accout,accout_lock accout记录用户名,密码 accout root 1qazxs ...
- 如何写java求和源代码
1.设计思想:利用eclipse编写. 2.程序流程图:先建立一个包->建立一个类->写代码->运行->修正错误,完善代码. 3.源程序代码: package dijia; p ...
- 【原创】QT简单计算器
代码 //main.cpp #include "calculator_111.h" #include <QtWidgets/QApplication> int main ...
- 【转载】《Learn More Study Less》整体性学习方法
原文 忘记在哪里看到这本书的介绍了,据说是一个小子自学1年,完成了4年麻省理工的课程,然后写了一本他学习方法的书.我搜了一下,居然中英文版都有,就花时间好好读了一遍,就是这本. 以下是这本书的完整笔记 ...
- (C/C++学习笔记) 三. 作用域和可见性
三. 作用域和可见性 ● 标识符的作用域 标识符的作用域是标识符在程序源代码中的有效范围,从小到大分为函数原型作用域.块作用域(局部作用域),文件作用域(全局作用域). 1. 函数原型作用域 函数原型 ...