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. uml图六种箭头的含义

    转:https://blog.csdn.net/wglla/article/details/52225571 在看一些技术博客的时候,经常会见到博客里画上很多uml图.因为经常会被这几种表达关系的箭头 ...

  2. T1飞跃树林 && 【最长等差子序列】

    solution by Mr.gtf 一道简单的递推 首先我们对树高从大到小排序 很容易得到递推式 ans[i]=Σans[j] (j<i && h[j]-h[i]<=K) ...

  3. 域名解析服务-DNS

    一.DNS概述 DNS(Domain Name System)即域名系统.它使用层次结构的命名系统.将域名和IP相互映射 在整个互联网环境中连接了数以亿计的服务器以及个人主机.其中大部分网站都使用了域 ...

  4. java代码之美(16) ---Java8 Optional

    Java8 Optional 一句话介绍Optional类:使用JDK8的Optional类来防止NullPointerException(空指针异常)问题. 一.前言 在我们开放过程中,碰到的异常中 ...

  5. Hanoi塔问题——递归

    /////////////Hanoi塔问题///////#include<iostream>using namespace std;void hanoi(int i,char A,char ...

  6. 基于 Serverless Component 全栈解决方案 Ⅱ

    虽然之前的文章 基于 Serverless Component 的全栈解决方案 介绍了如何借助 Serverless Component 快速搭建 Restful API 后端服务 和 Vue.js ...

  7. Software Testing Concepts

    Software Testing Concepts

  8. 用原生JS&PHP简单的AJAX实例

    功能介绍: 1)file.html 使用 xmlhttp 请求服务器端文件 text ,并更新 file.html 的部分内容 2)update.html 使用 xmlhttp 通过 filewrit ...

  9. 在虚拟机中使用NetToPLCSim和PLC相连.

    1,虚拟机...系统Win10...里面安装了VS. 2,本机...系统Win10...里面安装了博图15. 3,转换软件:NetToPLCSIM. 4,本机和虚拟机连接同一个路由器.注意: 5,设置 ...

  10. 二进制、十六进制理解及int类型二进制存储方式

    二进制 0000 0000 0000 0000 0000 0000 0000 0001 // 2^0 0000 0000 0000 0000 0000 0000 0000 0010 // 2^1 00 ...