python入门-WHILE循环
1 使用while循环
current_number=
while current_number <= :
print(current_number)
current_number +=
2 让用户选择退出
prompt = "tell me somthing, and i will repeat it back to you"
prompt = "\nEnter 'quit' to end the program" message = ""
while message != 'quit':
message=input(prompt) if message!='quit':
print(message)
3 使用标志
prompt = "tell me somthing, and i will repeat it back to you"
prompt = "\nEnter 'quit' to end the program" active = True
while active:
message = input(prompt) if message == 'quit':
active = False
else:
print(message)
4 使用break退出循环
prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(enter 'quit' when you are finished.)" while True:
city = input(prompt) if city == 'quit':
break
else:
print("I'd love to go to" + city.title() + "!")
5 在循环中使用continue
current_number =
while current_number < :
current_number +=
if current_number % ==:
continue print(current_number)
6 使用while循环来处理列表和字典
unconfirmed_users = ['alice','brian','candace']
confirmed_users = [] while unconfirmed_users:
current_user = unconfirmed_users.pop() print("verifying user:" + current_user.title())
confirmed_users.append(current_user) print("\n the following users have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
7 删除特定的列表元素
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets) while 'cat' in pets:
pets.remove('cat') print(pets)
8使用用户输入来填充字典
responses = {}
polling_active = True
while polling_active:
name = input("\n what is your name?")
response = input("which mountain would you like to climb someday")
responses[name] = response
repeat = input("would you like to let another person respond?(yes or no")
if repeat == 'no':
polling_active = False
print("\n --poll results--")
for name,response in responses.items():
print(name + "would like to climb" + response + ".")
python入门-WHILE循环的更多相关文章
- python入门10 循环语句
两种循环: 1 for in 2 while #coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 循环语句 ...
- 05 . Python入门值循环语句
一.Python循环语句 程序一般情况下是按照顺序执行的 编程语言提供了各种控制结构,允许更复杂的执行路径 Python中的循环语句有for和while但没有do while 循环语句允许我们执行一个 ...
- Python入门9 —— 循环
一:问号三连 1.什么是循环? 循环就是重复做一件事 2.为何要用循环? 为了让计算机能够像人一样去重复做事情 3.如何用循环 while循环,又称之为条件循环 for循环 二:循环 1.while循 ...
- Python入门-分支循环结构
编写代码的过程中,除了基本的变量,数据类型,在实际开发中,大量代码是根据判断条件,进而选择不同的的向前运行方式. 这些向前的运行方式基本分为两种:分支结构,循环结构 1.分支结构 if单分支结构 # ...
- python入门(11)条件判断和循环
python入门(11)条件判断和循环 条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: ag ...
- python入门学习:6.用户输入和while循环
python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...
- Python趣味入门5:循环语句while
跟着小牛叔,找准正确编程入门姿势,每天只要阅读10分钟. 任何语言都有循环语句,在Python里循环更是变化无穷,有基本的循环,有循环else语句,引伸出来的还有迭代器.推导式,咱们先学习最简单的一种 ...
- 大爽Python入门教程 3-3 循环:`for`、`while`
大爽Python入门公开课教案 点击查看教程总目录 for循环 可迭代对象iterable 不同于其他语言. python的for循环只能用于遍历 可迭代对象iterable 的项. 即只支持以下语法 ...
- 大爽Python入门教程 1-3 简单的循环与判断
大爽Python入门公开课教案 点击查看教程总目录 这里只初步认识下循环和判断,以便于我们去实现一些简单的计算. 循环和判断的详细知识和细节,我们将在后面的章节(大概是第三章)展开阐述. 1 初步了解 ...
随机推荐
- BZOJ3230: 相似子串【后缀数组】
Description Input 输入第1行,包含3个整数N,Q.Q代表询问组数. 第2行是字符串S. 接下来Q行,每行两个整数i和j.(1≤i≤j). Output 输出共Q行,每行一个数表示每组 ...
- unknown error: call function result missing 'value'
好好的脚本全部报错,遇到这种现在看一下自己Chrome浏览的版本号 然后去chromedriver官网上下载最新驱动文件 https://sites.google.com/a/chromium.org ...
- 51Nod 1002:数塔取数问题(DP)
1002 数塔取数问题 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 一个高度为N的由正整数组成的三角形,从上走到下,求经过的数字和的最大值. 每 ...
- 辞树的QAQ水题(字符串统计,思维)
思路:统计一串字符有多少个'A',并分别统计出每个'A'前后有多少'Q'.然后让每个'A'前后的'Q'相乘并相加就能得出结果了. 注意:数据的类型,卡了int,要用long long. 还有就是在pc ...
- excel导出: mac safari对application/x-msdownload的支持不佳
https://blog.csdn.net/lizeyang/article/details/8982155?locationNum=3&fps=1 http://tool.oschina.n ...
- android 图片解码显示流程
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jingxia2008/article/details/32327699 问题来源 android 能 ...
- 【转】每天一个linux命令(13):less 命令
原文网址:http://www.cnblogs.com/peida/archive/2012/11/05/2754477.html less 工具也是对文件或其它输出进行分页显示的工具,应该说是lin ...
- vue-echarts-v3 使用
github地址:https://github.com/xlsdg/vue-echarts-v3 官方说明:无论多少个组件代码里写 import IEcharts from 'vue-echarts- ...
- jsp 中变量作用域:pageScope、requestScope、sessionScope、applicationScope
jsp 中,变量的作用域,一共有4种: pageScope:表示变量只能在本页面使用. requestScope:表示变量能在本次请求中使用. sessionScope:表示变量能在本次会话中使用. ...
- Debug---Eclipse断点调试基础
1.进入debug模式(基础知识列表)1.设置断点 2.启动servers端的debug模式 3.运行程序,在后台遇到断点时,进入debug调试状态 ========================= ...