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. Gym 101630(NEERC 17) D.Designing the Toy

    题目大意:给出三视图方向上分别能看到的正方形数a,b,c(1<=a,b,c<=100),在-100<=x,y,z<=100的范围内构造出满足情况的一种正方体的摆放方式 做法很简 ...

  2. HDU 4666 最远曼哈顿距离

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4666 关于最远曼哈顿距离的介绍: http://blog.csdn.net/taozifish/ar ...

  3. 使用Visual Studio2013打开Visual Studio2015项目

    使用VS2013打开VS2015的项目,操作步骤如下: ======================================== ①用记事本或者Notepad++ 打开项目sln文件进行编辑, ...

  4. (6)time&datetime(时间模块)

    什么是时间模块 就是处理时间相关的功能 如用户注册的时间.统计程序运行的时间等 time 模块  计算机中有三种时间 1.时间戳 从1970年到今天,这个时间段中间经历的秒数 获取时间戳:time.t ...

  5. 【JVM】jvm至jstack命令

    一.介绍 jstack是java虚拟机自带的一种堆栈跟踪工具.jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项&qu ...

  6. Dataframe 新增一列, apply 通用方法

    df['c'] = df.apply(lambda row: 1 if row['a'] < 0 and row['b'] > 0 else 0, axis=1) apply 是一个好方法 ...

  7. sql server CLR

    1. 配置sql server 启用CLR 在SQL Server2005/2008里面,CLR默认是关闭的.可以使用如下SQL语句开启CLR. sp_configure 'show advanced ...

  8. 【转】每天一个linux命令(16):which命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/08/2759805.html 我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面 ...

  9. hashcode 知识点

    java中常用的哈希码的算法. Object类的hashCode.返回对象的内存地址经过处理后的结构,由于每个对象的内存地址都不一样,所以哈希码也不一样. String类的hashCode.根据Str ...

  10. Lock 和 synchronized 的区别

    Lock 和 synchronized 的区别 Lock是一个接口,而synchronized是Java中的关键字,synchronized是内置的语言实现: synchronized在发生异常时,会 ...