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循环的更多相关文章

  1. python入门10 循环语句

    两种循环: 1 for in 2 while #coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 循环语句 ...

  2. 05 . Python入门值循环语句

    一.Python循环语句 程序一般情况下是按照顺序执行的 编程语言提供了各种控制结构,允许更复杂的执行路径 Python中的循环语句有for和while但没有do while 循环语句允许我们执行一个 ...

  3. Python入门9 —— 循环

    一:问号三连 1.什么是循环? 循环就是重复做一件事 2.为何要用循环? 为了让计算机能够像人一样去重复做事情 3.如何用循环 while循环,又称之为条件循环 for循环 二:循环 1.while循 ...

  4. Python入门-分支循环结构

    编写代码的过程中,除了基本的变量,数据类型,在实际开发中,大量代码是根据判断条件,进而选择不同的的向前运行方式. 这些向前的运行方式基本分为两种:分支结构,循环结构 1.分支结构 if单分支结构 # ...

  5. python入门(11)条件判断和循环

    python入门(11)条件判断和循环 条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: ag ...

  6. python入门学习:6.用户输入和while循环

    python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...

  7. Python趣味入门5:循环语句while

    跟着小牛叔,找准正确编程入门姿势,每天只要阅读10分钟. 任何语言都有循环语句,在Python里循环更是变化无穷,有基本的循环,有循环else语句,引伸出来的还有迭代器.推导式,咱们先学习最简单的一种 ...

  8. 大爽Python入门教程 3-3 循环:`for`、`while`

    大爽Python入门公开课教案 点击查看教程总目录 for循环 可迭代对象iterable 不同于其他语言. python的for循环只能用于遍历 可迭代对象iterable 的项. 即只支持以下语法 ...

  9. 大爽Python入门教程 1-3 简单的循环与判断

    大爽Python入门公开课教案 点击查看教程总目录 这里只初步认识下循环和判断,以便于我们去实现一些简单的计算. 循环和判断的详细知识和细节,我们将在后面的章节(大概是第三章)展开阐述. 1 初步了解 ...

随机推荐

  1. Roslyn编译器-C#动态脚本实现方案

    [前言] Roslyn 是微软公司开源的 .NET 编译器. 编译器支持 C# 和 Visual Basic 代码编译,并提供丰富的代码分析 API. Roslyn不仅仅可以直接编译输出,难能可贵的就 ...

  2. Where is Silverlight now?

    Some time ago, I wrote an article about the comparison between HTML5 and Silverlight. That article w ...

  3. 通过直接编码添加折线图到ChartControl

    https://documentation.devexpress.com/#WindowsForms/CustomDocument2976 ChartControl lineChart = new C ...

  4. ASP.NET MVC Area 的使用

    MVC提供Area机制,在同一个项目之内就能够切割出不同的ASP.NET MVC网站. 插入:首先在相同的位置,比如说同一个文件夹(如:Controllers)是不能创建俩个相同名称的文件(如:Hom ...

  5. smarty中调用php内置函数

    http://blog.csdn.net/clevercode/article/details/50373633

  6. jsfl读取xml,图片,并生成swf

    var newdoc = fl.createDocument(); var doc = fl.getDocumentDOM(); var URI = fl.browseForFolderURL(&qu ...

  7. 使用python生成词云

    什么是词云呢? 词云就是一些关键词组成的一个图片.大家在网上经常看到,下面看一些例子: 那用python生成一个词云的话怎么办呢,首先要有一些词,咱们随便找个吧,用see you again的歌词好了 ...

  8. hbase 知识点

    hbase 教程:http://www.yiibai.com/hbase/ mac下hbase安装:https://www.jianshu.com/p/510e1d599123 HBase是建立在Ha ...

  9. docker 使用教程(2)常用命令

    1. 查看docker信息(version.info) # 查看docker版本$docker version # 显示docker系统的信息$docker info 2. 对image的操作(sea ...

  10. asp.net core控制台项目运行

    cmd中进入项目生成的dll目录下 运行命令: start dotnet xxx.dll