input()  获取用户输入(获取的都是字符串哦)  //函数input()让程序停止运行,等待用户输入一些文本。

//不同于C的是可在input中添加用户提示,而scanf不具备这一特性。

//提示超过一行时可将提示储存在一个变量中再传递给input()

continue  忽略循环中余下代码,并返回到当前循环开头

break  退出当前循环,执行当前循环外的代码

一个小例子:

 1 prompt = "If you tell us who you are, we can pe rsonalize the messages you see."
2 prompt += "\nWhat is your firstname: "
3 active = True#控制循环
4 while active:
5 name = input(prompt)
6 if name == 'quit':#控制循环
7 active = False
8 #可替换为break,结束循环
9 #换为continue 则成了死循环
10 else :
11 print("Hello!" + name + "!")
12
13 print('4')#测试是否跳出循环

* Python 2.7中 使用 raw_input()  获取用户输入

使用while循环来处理列表和字典

一个简单的移动列表:

1 unconfirmed_users =['alice', 'brain', 'cand', 'ace',]
2 confirmed_users =[]
3 while unconfirmed_users:
4 current_user = unconfirmed_users.pop()#弹出并记录
5 print("Verifying user: " + current_user.title())
6 confirmed_users.append(current_user)#添加到新列表中
7 print("\nThe following users have been confirmed: ")
8 for confirmed_user in confirmed_users:#显示已认证用户
9 print(confirmed_user.title())

删除列表中特定值的所有元素:

1 while 'alice' in unconfirmed_users#只要unconfirmed_users列表中存在alice这个元素就一直循环
2 unconfirmed_users.remove('alice')#删除unconfirmed_users列表中的alice元素

使用用户输入填充字典:

 1 names = {}
2 while 1:
3 name = input("What's your name : ")
4 if name == 'no':#强迫输入哈哈哈。
5 continue
6 day = input("How old are you: ")
7 if day == 'no':
8 continue
9 #以上俩if可采取if-elif
10 # if name == no:
11 # continue
12 # elif day == no:
13 # continue
14 names[name] = day#自动添加键值对
15 #alien_0['x_position'] = 0 为字典添加键值对
16 #alien_0['x_position'] =25 修改字典中的值
17 repeat = input("Would you like to let an other person respond?(yes/no)")
18 if repeat == 'no':
19 break
20 for a, b in names.items():#打印键值对
21 print(a.title() + b)

Python 用户输入&while循环 初学者笔记的更多相关文章

  1. 【PY从0到1】第六节 用户输入while循环

    # 6 第六节 用户输入while循环 # 1> 重要的函数--input() # 我们先讲解一下input():当Python碰到input()后会执行括号内的语句. # 随后等待用户的输入. ...

  2. Python用户输入和代码注释

    一.用户输入 若你安装的是Python3.x版本,当你在Python IDLE(编辑器) 中输入以下代码: name = input('用户名:') print('Hello',name) 保存并执行 ...

  3. 初入python 用户输入,if,(while 循环)

    python 基础 编译型: 一次性将所有程序编译成二进制文件. 缺点:开发效率低,不能跨平台 优点:运行速度快. :c ,c++语言 等等.... 解释行:当程序执行时,一行一行的解释. 优点:开发 ...

  4. python ---用户输入

    范例1:我们希望整数(整数),这就是为什么我们使用int()函数. x = int(raw_input("Enter x:")) y = int(raw_input("E ...

  5. Python用户终端输入

    #用户输入,操作 print("python 用户输入操作") # input(提示字符串),函数阻塞程序,并提醒用户输入字符串 instr = input("pleas ...

  6. Python编程从入门到实践笔记——用户输入和while循环

    Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...

  7. 读书笔记「Python编程:从入门到实践」_7.用户输入和while循环

    7.1 函数input()的工作原理 函数input() 让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. message = input(&qu ...

  8. python学习笔记(四)---用户输入与while循环

    用户输入 函数input demo1: message = input("all you input is chars:") print(message) demo2: 由inpu ...

  9. Python3基础(1)Python介绍、Python2 与Python3、变量、用户输入、if...else和for循环、while循环、break与continue

    ---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ P ...

随机推荐

  1. 【译文连载】 理解Istio服务网格(第一章 概述)

    书籍英文版下载链接为 https://developers.redhat.com/books/introducing-istio-service-mesh-microservices/,作者 Burr ...

  2. 《Head First Java(第二版)》中文版 分享下载

    书籍信息 书名:<Head First Java(第二版)>中文版 作者: Kathy Sierra,Bert Bates 著 / 杨尊一 编译 张然等 改编 豆瓣评分:8.7分 内容简介 ...

  3. k8s系列---Service之ExternalName用法

    需求:需要两个不同的namespace之间的不同pod可以通过name的形式访问 实现方式: A:在其他pod内ping [svcname].[namespace] ping出来到结果就是svc的ip ...

  4. Django (二) 常用字段及 ORM

    MVC介绍 Django生命周期 many-to-many One-to-many Django常用字段 CharFiled 需要有max_length unique=True(代表不能重名) Ema ...

  5. 第三篇 SpringBoot整合log4j2详解

    源代码:https://pan.baidu.com/s/1d1Lwv1gIvVNltIKVWeEseA 提取码:wff0 SpringBoot整合Log4j2步骤: 1.删除spring-boot-s ...

  6. linux中的挂载命令

    一.查询与自动挂载 查询系统中已经挂载的设备,-l会显示卷标名称 mount [-l] oot@izm5e2q95pbpe1hh0kkwoiz tmp]# mount sysfs on /sys ty ...

  7. 多线程共享变量和 AsyncLocal

    >>返回<C# 并发编程> 1. 简介 2. 异步下的共享变量 3. 解析 AsyncLocal 3.1. IAsyncLocalValueMap 的实现 3.2. 结论 1. ...

  8. 戏说前端之CSS编码规范

    前言 项目启动时 css 应该注意哪些问题 文件名规范 文件名建议用小写字母加中横线的方式.为什么呢?因为这样可读性比较强,看起来比较清爽,像链接也是用这样的方式,例如 // 地址: github的地 ...

  9. Android中使用AlertDialog实现几种不同的对话框

    场景 app中常见的对话框. 简单的带确定取消按钮的对话框 带列表的对话框 带单项选择的对话框 带多项选择的对话框 注: 博客: https://blog.csdn.net/badao_liumang ...

  10. luogu2173 [ZJOI2012]网络

    题目链接 problem 给出一个无向图,每条边有一种颜色.每种颜色都构成一个森林.需要完成以下操作. 修改点权 修改边的颜色 询问某种颜色的森林中某条路径上点权最大值 solution 颜色数量不超 ...