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 初步了解 ...
随机推荐
- 1010. Radix (25) pat
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- 今天遇到的一个奇葩的NoClassFound的问题
nohup的日志中报错 java.lang.NoClassDefFoundError: org/apache/catalina/core/ApplicationContext$DispatchData ...
- tarjan强联通分量(模板)
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #inc ...
- LeetCode-Microsoft-Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- 干了2个月java开发最深的体会
综上为JQuery中ajax的操作,感觉使用多了,和form表单的处理还是非常相似的,只不过实现的功能不一样罢了.学习编程,其实就是学习对数据的流转处理,也即是程序的运行流程,如何从前台获取,传输到服 ...
- 用vbs打开文件
set ws=createobject("wscript.shell")ws.run"打开文件所使用的程序+空格+程序路径",x,ture Run方法有三个参数 ...
- Bloom Filter(布隆过滤器)的概念和原理
Bloom filter 适用范围:可以用来实现数据字典,进行数据的判重,或者集合求交集 基本原理及要点: 对于原理来说很简单,位数组+k个独立hash函数.将hash函数对应的值的位数组置1,查找时 ...
- JVM(下)
持久代:不会被 gc 给轻易回收的,创建后一直存在,持久代在堆内存里面,但是不归 java 程序使用.持久代是 动态 load 的那些 class,局部变量,去 gc 其实也 gc 不了啥 1.8 之 ...
- FastAdmin 离线安装 ueditor 出现 rule 错误
使用的是 phpStudy 的 nginx + php5.6 离线安装 ueditor.zip 出现,安装其它的插件没有问题. Call to a member function rule() on ...
- 【转】每天一个linux命令(20):find命令之exec
原文网址:http://www.cnblogs.com/peida/archive/2012/11/14/2769248.html find是我们很常用的一个Linux命令,但是我们一般查找出来的并不 ...