day4 四、流程控制之if判断、while循环、for循环
一、if判断
1、语法一:
if 条件:
条件成立时执行的子代码块
代码1
代码2
代码3
示例:
sex='female'
age=
is_beautiful=True
and age < and is_beautiful:
print('开始表白。。。')
print('other code1...')
print('other code2...')
print('other code3...')
2、语法二:
if 条件:
条件成立时执行的子代码块
代码1
代码2
代码3
else:
条件不成立时执行的子代码块
代码1
代码2
代码3
示例:
sex='female'
age=
is_beautiful=True
and age < and is_beautiful:
print('开始表白。。。')
else:
print('阿姨好。。。') 阿姨好。。。
print('other code1...')
print('other code2...')
print('other code3...')
3、语法三:
if 条件1:
if 条件2:
代码1
代码2
代码3
示例:
sex='female'
age=
is_beautiful=True
is_successful=True
height=1.70
and age < 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...')
4、语法四:
if 条件1:
代码1
代码2
代码3
elif 条件2:
代码1
代码2
代码3
elif 条件3:
代码1
代码2
代码3
.......
else:
代码1
代码2
代码3
示例:
如果成绩 >= ,那么:优秀
如果成绩 >= 80且 < , 那么:良好
如果成绩 >= 70且 < , 那么:普通
其他情况:很差
score = input('
score = int(score)
:
print('优秀')
elif score >= :
print('良好')
elif score >= :
print('普通')
else:
print('很差')
二、while循环。while可以嵌套使用,以下案例有解释说明。
语法:
while 条件:
代码1
代码2
代码3
例如:
while True:
name=input('please input your name: ')
pwd=input('please input your password: ')
':
print('login successful')
else:
print('username or password error')
# while True:
# name = input('我的暗号是:>>>')
# password = input('请输入下一句:>>>')
# if name == '请问今天你带烟了吗' and password == '我已经戒烟几年了':
# print('输入正确,你是自己人,我是中共地下党情报组组长,你的上司')
# else:
# print('输入错误,你是间谍,就地拿下')
# 打印出1-10个数
# count =
# :
# print(count)
# count +=
1、结束while循环的两种方式:
方式一:条件改为false:条件mag改为False。条件改为False时,不会立即结束本层循环,是在下一次循环判断条件时才会生效。
# 方式一:条件mag改为False。
# 条件改为False时,不会立即结束本层循环,是在下一次循环判断条件时才会生效。
# mag = True
# while mag:
# name = input('我的暗号是:>>>')
# password = input('请输入下一句:>>>')
# if name == '请问今天你带烟了吗' and password == '我已经戒烟几年了':
# print('输入正确,你是自己人,我是中共地下党情报组组长,你的上司')
# while mag:
# print("""
# 会话结束
# 你现在正在进行的工作
# 你的名字
# 你今天洗澡了吗
# """)
# choice = input('你想知道的我都可以告诉你,你问吧')
# ':
# mag = False
# elif choice == ':
# print('你现在正在进行的工作')
# elif choice == ':
# print('你的名字')
# elif choice == ':
# print('你今天洗澡了吗')
# else:
# print('我不想回答')
#
# else:
# print('输入错误,你是间谍,就地拿下')
方式二:while + break:break 一定是在循环体内,当程序运行到break时,就会立刻结束本层循环。
# while True:
# name = input('我的暗号是:>>>')
# password = input('请输入下一句:>>>')
# if name == '请问今天你带烟了吗' and password == '我已经戒烟几年了':
# print('输入正确,你是自己人,我是中共地下党情报组组长,你的上司')
# while True:
# print("""
# 会话结束
# 你现在正在进行的工作
# 你的名字
# 你今天洗澡了吗
# """)
# choice = input('你想知道的我都可以告诉你,你问吧')
# ':
# break
# elif choice == ':
# print('你现在正在进行的工作')
# elif choice == ':
# print('你的名字')
# elif choice == ':
# print('你今天洗澡了吗')
# else:
# print('我不想回答')
# break
# else:
# print('输入错误,你是间谍,就地拿下')
while + continue:是在循环体内结束本次循环,直接执行下一次循环。
# 打印出1-10个数 # count = # : # print(count) # count += # 打印1-,-,不打印6和7 # count = # : # or count == : # continue # print(count) # count +=
三、for循环:for 循环能解决的,while循环都能解决。
但是为什么还要使用for循环呢,是因为在特定的情况下,使用for循环更简便一些。
它的强大之处在于能循环取值。
# l = ['a', 'b', 'c', 'd', 'e'] # i = # while i < len(l): # print(l[i]) # i += # 结果为 # b # c # d # e # name = ['jerry', 'tommy', 'anne', 'una'] # for x in name: # print(x)
1、for + break
# name = ['jerry', 'tommy', 'anne', 'una'] # for x in name: # if x == 'anne': # break # print(x)
2、for + continue
# name = ['jerry', 'tommy', 'anne', 'una'] # for x in name: # if x == 'anne': # continue # print(x)
3、for + else
# name = ['jerry', 'tommy', 'anne', 'una']
# for x in name:
# if x == 'zoe':
# break
# print(x)
#
# else:
# print('其他')
4、for + range
# for + range # 顾头不顾尾 # , ): # print(x) # 结果为 # # # # #
5、for 嵌套:顾头不顾尾。range(1,5) 取值为 1 2 3 4
# , ): # , ): # print(x, y) # 结果为 # # # # # # # #
四、课后作业
1、实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!
name = input('请输入用户名:>>>')
password = input('请输入密码:>>>')
':
print('登录成功')
else:
print('登录失败')
2、实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
count =
:
name = input('请输入用户名:>>>')
password = input('请输入密码:>>>')
':
print('登录成功')
break
else:
print('登录失败')
count +=
3、求1-2+3-4+5 ... 99的所有数的和
res =
count =
:
== :
res -= count
else:
res += count
count +=
print(res)
4、使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12
count =
:
or count == :
count +=
continue
print(count)
count +=
5、使用 while 循环实现输出 1-100 内的所有奇数
count =
:
== :
print(count)
count +=
使用 while 循环实现输出 1-100 内的所有奇数
count =
:
== :
print(count)
count +=
6、用户登陆(三次机会重试)
count=
:
name=input('请输入用户名:')
password=input('请输入密码:')
':
print('login success')
break
else:
print('用户名或者密码错误')
count+=
7、猜年龄游戏
age_of_oldboy=
count=
:
guess=int(input('>>: '))
if guess == age_of_oldboy:
print('you got it')
break
count+=
8、猜年龄游戏升级版
要求: 允许用户最多尝试3次 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序 如何猜对了,就直接退出
age_of_oldboy=
count=
while True:
:
choice=input('继续(Y/N?)>>: ')
if choice == 'Y' or choice == 'y':
count=
else:
break
guess=int(input('>>: '))
if guess == age_of_oldboy:
print('you got it')
break
count+=
day4 四、流程控制之if判断、while循环、for循环的更多相关文章
- 基础运算符补充,流程控制之if判断/while循环
常量 常量即指不变的量.在python中没有一个专门 的语法代表常量,程序员约定俗成地用变量名全部被大写代表常量. AGE_OF_OLDBOY = 56 基础运算符补充 1.算术运算 加减乘除+ - ...
- 格式化输出的三种方式,运算符及流程控制之if判断
''' 格式化输出的三种方式,运算符及流程控制之if判断 ''' # 格式化输出的三种方式 # 一.占位符 程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式 比如要求用户输入用户名和年龄 ...
- [基本运算符、流程控制之if判断、与用户交互、深浅拷贝]
[基本运算符.流程控制之if判断.与用户交互] 基本运算符 1.算数运算符 python支持的算术运算符与数学上计算的符号使用是一致的 salary = 3.3 res = salary * 12 p ...
- C 碎片四 流程控制
前面介绍了程序中用到的一些基本要素(常量,变量,运算符,表达式),他们是构成程序的基本成分,下面将介绍C语言中流程控制的三种结构:顺序结构.分支结构.循环结构 一.顺序结构 顺序结构的程序设计是最简单 ...
- java 基础知识四 流程控制
java 基础知识四 流程控制 Java流程控制包括顺序控制.条件控制和循环控制 顺序控制就是逐条执行 有if和switch两个分支 循环控制就是 又称为回路控制,根据循环初始条件和终结要求,执行 ...
- python小白——进阶之路——day4天-———流程控制while if循环
# ### 代码块: 以冒号作为开始,用缩进来划分作用域,这个整体叫做代码块 if 5 == 5: print(1) print(2) # 注意点: 要么全部使用4个空格,要么全部使用1个缩进 ,这样 ...
- 流程控制之if判断,while循环,for循环
if判断? 什么是if判断? 判断一个条件如果成立则做...不成立则... 为什么要有判断? 让计算机像人一样具备判断的能力 如何用if判断 if 条件1: code1 code2 cod ...
- Java学习day4 程序流程控制一
一.分支结构 条件语句:if...else if语句: 一个 if 语句包含一个布尔表达式和一条或多条语句,如果布尔表达式的值为 true,则执行 if 语句中的代码块,否则执行 if 语句块后面的代 ...
- 流程控制之if判断
目录 语法(掌握) if if...else if...elif...else 练习(掌握) 练习1:成绩评判 练习2:模拟登录注册 if的嵌套(掌握) 语法(掌握) if判断是干什么的呢?if判断其 ...
随机推荐
- 求标准分sql
if object_id('tempdb..#tempTable') is not null Begin drop table #tempTable End [校区],[学年],[考试年级],[考试类 ...
- PL/SQL学习笔记之存储过程
一:PL/SQL的两种子程序 子程序:子程序是执行一个特定功能.任务的程序模块.PL/SQL中有两种子程序:函数 和 过程. 函数:主要用于计算并返回一个值. 过程:没有直接返回值,主要用于执行操 ...
- Cache Line 伪共享发现与优化
https://yq.aliyun.com/articles/465504 Cache Line 伪共享发现与优化 作者:吴一昊,杨勇 1. 关于本文 本文基于 Joe Mario 的一篇博客 改编而 ...
- tmux手册中文翻译
man tmux可以看到最详细的tmux介绍,本文翻译自tmux手册. tmux全名叫"terminal multiplexer",终端多路复用器. tmux的命令格式为: tmu ...
- NDK配置
NDK 配置 Android SDK中下载NDK, LLDB Android.mk 和 Application.mk 简单来说 Android.mk 用来描述需要生成哪些模块的 .so 文件 Appl ...
- MUI class="mui-switch" 开关监听
如何对MUI中的switch开关按钮进行监听, 页面代码如下: <form class="mui-input-group"> <ul class="mu ...
- Netflix开源类库archaius(一)概述
archaius是什么,能做什么? archaius是Netflix公司开源项目之一,基于java的配置管理类库,主要用于多配置存储的动态获取.主要功能是对apache common configur ...
- tensorflow 笔记11:tf.nn.dropout() 的使用
tf.nn.dropout:函数官网说明: tf.nn.dropout( x, keep_prob, noise_shape=None, seed=None, name=None ) Defined ...
- Core dump去哪里了?
转自:http://blog.csdn.net/normallife/article/details/53818997 今天程序Crash,去追踪,找core dump,始终没有找到,后来到了/pro ...
- Android Launcher分析和修改1——Launcher默认界面配置(default_workspace)
最近工作都在修改Launcher,所以打算把分析源码和修改源码的过程记录下来,最近会写一些关于Launcher的分析和修改博文.因为我是修改4.0.3的Launcher,所以后面文章里面的Launch ...