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判断其 ...
随机推荐
- 使用JavaCV播放视频、摄像头、人脸识别
一.导入Maven依赖包 <dependencies> <!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-pla ...
- C# System.IO.File
using System; using System.IO; class Test { public static void Main() { string path = @"c:\temp ...
- vim Google style format
https://github.com/google/vim-codefmt https://github.com/rhysd/vim-clang-format http://pre.tir.tw/00 ...
- Android——SeekBar(拖动条)相关知识总结贴
Android进度条(ProgressBar)拖动条(SeekBar)星级滑块(RatingBar)的例子 http://www.apkbus.com/android-51326-1-1.html A ...
- Python gensim库word2vec 基本用法
ip install gensim安装好库后,即可导入使用: 1.训练模型定义 from gensim.models import Word2Vec model = Word2Vec(senten ...
- 译:1. RabbitMQ Java Client 之 "Hello World"
这些教程介绍了使用RabbitMQ创建消息传递应用程序的基础知识.您需要安装RabbitMQ服务器才能完成教程 1. 打造第一个Hello World 程序 RabbitMQ是一个消息代理:它接受和转 ...
- [k8s]jenkins部署在k8s集群
$ cat jenkins-pvc.yaml kind: PersistentVolumeClaim apiVersion: v1 metadata: name: jenkins-pvc spec: ...
- Socket网络编程--聊天程序(2)
上一节简单如何通过Socket创建一个连接,然后进行通信.只是每个人只能说一句话.而且还是必须说完才会接收到信息,总之是很不方便的事情.所以这一小节我们将对上一次的程序进行修改,修改成每个人可以多说话 ...
- [Big Data - Kafka] Kafka设计解析(四):Kafka Consumer解析
High Level Consumer 很多时候,客户程序只是希望从Kafka读取数据,不太关心消息offset的处理.同时也希望提供一些语义,例如同一条消息只被某一个Consumer消费(单播)或被 ...
- FAL_CLIENT和FAL_SERVER参数详解
FAL_CLIENT和FAL_SERVER参数详解 转载:http://openwares.net/database/fal_client_fal_server.html FAL_CLIENT和FAL ...