Python 用户输入&while循环 初学者笔记
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循环 初学者笔记的更多相关文章
- 【PY从0到1】第六节 用户输入while循环
# 6 第六节 用户输入while循环 # 1> 重要的函数--input() # 我们先讲解一下input():当Python碰到input()后会执行括号内的语句. # 随后等待用户的输入. ...
- Python用户输入和代码注释
一.用户输入 若你安装的是Python3.x版本,当你在Python IDLE(编辑器) 中输入以下代码: name = input('用户名:') print('Hello',name) 保存并执行 ...
- 初入python 用户输入,if,(while 循环)
python 基础 编译型: 一次性将所有程序编译成二进制文件. 缺点:开发效率低,不能跨平台 优点:运行速度快. :c ,c++语言 等等.... 解释行:当程序执行时,一行一行的解释. 优点:开发 ...
- python ---用户输入
范例1:我们希望整数(整数),这就是为什么我们使用int()函数. x = int(raw_input("Enter x:")) y = int(raw_input("E ...
- Python用户终端输入
#用户输入,操作 print("python 用户输入操作") # input(提示字符串),函数阻塞程序,并提醒用户输入字符串 instr = input("pleas ...
- Python编程从入门到实践笔记——用户输入和while循环
Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...
- 读书笔记「Python编程:从入门到实践」_7.用户输入和while循环
7.1 函数input()的工作原理 函数input() 让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. message = input(&qu ...
- python学习笔记(四)---用户输入与while循环
用户输入 函数input demo1: message = input("all you input is chars:") print(message) demo2: 由inpu ...
- Python3基础(1)Python介绍、Python2 与Python3、变量、用户输入、if...else和for循环、while循环、break与continue
---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ P ...
随机推荐
- Git的指令
一,访问本地Git 上一节我们已学会了如何注册GitHub和安装Git 现在先打开电脑终端或Git Bash,首先和Git打个招呼,输入Git 二.新建文件/进入文件夹 mkdir + 文件名 ...
- 二维数组转稀疏数组、稀疏数组恢复二维数组(Java实现)
public static void main(String[] args) { // 创建一个原始的二维数组 9*9 int chessArr1[][] = new int[9][9]; // 0表 ...
- Import This - The Zen of Python
The Zen of Python -- by Tim Peters Beautiful is better than ugly.Explicit is better than implicit.Si ...
- Python 协程 - Coroutines
协程 - Coroutines Awaitable Objects, Awaitable Objects 通常由 __await__() 生成, 而 Coroutine objects 是由 asyn ...
- windows本地安装以及使用Jenkins
首先保证本地已安装JDK,Jenkins是需要Java开发的,需要JDK运行环境,此处不提供,网上一大把. 一.Jenkin安装: 下载Jekins,到官网下载,https://jenkins.io/ ...
- lwip eth插拔网线自动维护接口状态
硬件连线就是将dp83848的INT脚连到STM32的某个中断脚上,这里是PB14 PB14的中断处理函数中,会释放一个信号量,这里只是发生链路状态改变中断(网线插上或拔下) void EXTI15_ ...
- FTP的连接方式(防火墙的配置)
FTP是仅基于TCP的服务,不支持UDP.与众不同的是FTP使用2个端口,一个数据端口和一个命令端口(也可叫做控制端口).通常来说这两个端口是21(命令端口)和20(数据端口). ...
- python2 + Django 中文传到模板页面变Unicode乱码问题
1.确保views页面首行设置了默认编码 # -*-coding:utf-8 -*- 2.确保html页面的编码为 utf-8 3.确保项目setting文件设置了 LANGUAGE_CODE = ...
- 部署LAMP环境搭建一个网站论坛平台
修改主机名 Hostname openstack-001 Hostname Login 修改本地域名解析 Vi /etc/hosts 最后一行添加 192.168.1.56 openstack-001 ...
- href的几个特殊值
a href ="" :默认打开的还是当前页面,会刷新一下重新打开. a href ="#": 浏览器地址栏网址后面会多显示1个#.不会刷新页面,会回到页面顶部 ...