《Python编程从入门到实践》_第七章_用户输入和whlie循环
函数input()的工作原理
函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,python将其存储在一个变量中,以方便你使用。
#输入用户名
username = input("Please input your username:")
print (username)
#运行结果
Please input your username:Frank
Frank
变量传递给函数input()
有的时候,我们需要提示的字符串比较长,可以将提示的语句放在一个变量里面。
#greeter
prompt = "If you tell us who you are,we can personalize the messages you see."
prompt += "\nWhat is your first name?"
name = input(prompt)
print ("\nhello, " + name + "!")
#运行结果
If you tell us who you are,we can personalize the messages you see.
What is your first name?Frank
hello, Frank!
使用int()来获取数值输入
我们input()所得到的是字符串数据,包括你想输入的整型是123,但是保存到变量里面的时候却是字符串"123"。
#判断年龄是否达到做过山车的年龄
age = input("How old are you?")
if age >=18:
print("You can ride!")
else:
print("You can't ride ")
#运行结果,当我们不使用int()把字符串转为整型的话,age是不能和数值18比较的
TypeError: '>=' not supported between instances of 'str' and 'int'
所以需要使用函数int()
#判断年龄是否达到做过山车的年龄
age = input("How old are you?")
if int(age) >=18:
print("You can ride!")
else:
print("You can't ride ")
#运行结果
How old are you?18
You can ride!
求模运算符
处理数值信息时,求模运算符(%)是一个很有用的工具,它将两个数相除并返回余数:
>>> 4 % 3
1
>>> 5 % 3
2
>>> 6 % 3
0
>>> 7 % 2
1
可以用来判断奇偶数。
python2中的raw_input()
在python2中使用raw_iput()来提示用户输入,这个与python3中的input()是一样的,也将输入解读为字符串。python2中也存在input(),但它将用户输入解读为python代码,并尝试执行它。
username = raw_input("Please input your username:")
print (username)
username = input("Please input your username:")
print (username)
#运行结果
Please input your username:cisco
cisco
Please input your username:cisco
Traceback (most recent call last):
File "C:\Python27\test.py", line 3, in <module>
username = input("Please input your username:")
File "<string>", line 1, in <module>
NameError: name 'cisco' is not defined
我们会看到在python2中使用raw_input可以正常输出,但是使用input就不能正常输出的,因为他把你输入的当做可执行的代码。
while循环
while循环不断地运行,直到指定的条件不满足为止。
#输出1-5
current_number = 1
while current_number <= 5:
print(current_number)
current_number+=1
#运行结果
1
2
3
4
5
使用标志
有的时候使用标志可以简化while的语句,因为不需要在其中做任何比较--相关的逻辑由程序的其他部分处理。
#quit退出
prompt = "\nTell me something,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)
#运行结果
Tell me something,and i will repeat it back to you:
Enter 'quit' to end the program. hello!
hello!
Tell me something,and i will repeat it back to you:
Enter 'quit' to end the program. my name is Frank
my name is Frank
Tell me something,and i will repeat it back to you:
Enter 'quit' to end the program. quit
break和continue
#break举例
current_number = 1
while current_number < 10:
if current_number == 5:
current_number+=1
break
else:
print(current_number)
current_number+=1
#运行结果
1
2
3
4
#continue举例
current_number = 1
while current_number < 10:
if current_number == 5:
current_number+=1
continue
else:
print(current_number)
current_number+=1
#运行结果
1
2
3
4
6
7
8
9
《Python编程从入门到实践》_第七章_用户输入和whlie循环的更多相关文章
- 《Python编程从入门到实践》第三章_列表简介
什么是列表呢? 官方说明就是由一些列按特点顺序排列的元素组成.其实可以看出很多个字符串的有序组合吧,里面的内容可以随时的删除,增加,修改. 下面这个就是一个列表,python打印列表的时候会将中括号和 ...
- 《python编程从入门到实践》第七章笔记
用户输入和while循环 1.函数input():让程序停止运行,等待用户输入一些文本.接受一个参数,既即要向用户显示的提示或说明. 2.将数值输入用于计算和比较前,务必将其转换为数值表示. 3.fo ...
- 《python编程从入门到实践》第六章笔记
1.字典 字典:一系列键-值对,每一个键都与每一个值相关联.与键相关联的值可以是数字.字符串.列表和字典. 最简单的字典只有一个键值对. eg: alien = {'color':'green','p ...
- 《Python编程从入门到实践》_第十章_文件和异常
读取整个文件 文件pi_digits.txt #文件pi_digits.txt 3.1415926535 8979323846 2643383279 下面的程序打开并读取整个文件,再将其内容显示到屏幕 ...
- 《python编程从入门到实践》读书实践笔记(一)
本文是<python编程从入门到实践>读书实践笔记1~10章的内容,主要包含安装.基础类型.函数.类.文件读写及异常的内容. 1 起步 1.1 搭建环境 1.1.1 Python 版本选择 ...
- Python编程从入门到实践笔记——异常和存储数据
Python编程从入门到实践笔记——异常和存储数据 #coding=gbk #Python编程从入门到实践笔记——异常和存储数据 #10.3异常 #Python使用被称为异常的特殊对象来管理程序执行期 ...
- Python编程从入门到实践笔记——文件
Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...
- Python编程从入门到实践笔记——类
Python编程从入门到实践笔记——类 #coding=gbk #Python编程从入门到实践笔记——类 #9.1创建和使用类 #1.创建Dog类 class Dog():#类名首字母大写 " ...
- Python编程从入门到实践笔记——函数
Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...
- Python编程从入门到实践笔记——用户输入和while循环
Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...
随机推荐
- Insus.NET最近想更换一部手机
Insus.NET曾经使用过好几部手机.给Insus.NET工作与生活上带来了方便.最近想更换一部新手机,因此记念一下以前使用过的手机.当时Insus.NET没有相机,下面图片是网上找的(前四部): ...
- JavaScript中创建自定义对象的方法
本文内容参考JavaScript高级程序设计(第3版)第6章:面向对象的程序设计 ECMA-262中把对象定义为:“无序属性的集合,其属性可以包含基本值.对象或者函数.”我所理解的就是对象就是一个结构 ...
- vs2015+opencv3.3.1 +Eigen 3.3.4 c++ 实现 泊松图像编辑(无缝融合)
#define EIGEN_USE_MKL_ALL #define EIGEN_VECTORIZE_SSE4_2 #include <iostream> #include "co ...
- 强制所有网页链接在同一页面打开或者在TabControl中弹出新窗口
IEwebbrowser中老生常谈的话题. 一般的解决都是通过 // webBrowser.Navigating += WebBrowser_Navigating; 注册转跳前事件 private v ...
- Java以邮件附件的方式发送excel文件
String to = "xxx@qq.com"; // 收件人的QQ邮箱 String from = "xxx@qq.com"; // 发件人的QQ邮箱 St ...
- The method identifyUser(Arrays.asList("group001"), String, new HashMap<>()) is undefined for the type AipFace
在使用百度云的人脸识别sdk时遇到了这个错误,网上百度不到解决的方法,当我浏览百度云的时候发现了这个 于是考虑到版本可能更新,出现了新的函数代替旧的函数,于是去查文档,文档链接如下 https://c ...
- 使用itchat监控微信消息,从此不再为撤回烦恼
强大的Itchat itchat是一个开源的微信个人号接口,使用python封装接入微信网页版接口,通过调用itchat来登录微信网页版收发消息. 项目简介 - itchat 掌握itchat之后,只 ...
- centos7 docker 安装 zookeeper 3.4.13 集群
假设三台主机的ip分别为: 主机一:192.168.0.168 主机二:192.168.0.169 主机三:192.168.0.170 三台主机的安装步骤相似,以主机一为例: 1. 查找zookeep ...
- Oracle Secure Backup设置Infiniband网络优先
默认情况下,Oracle Secure Backup备份软件走管理网进行数据备份,如果需要Infiniband网络进行备份,则必须设置Preferred Network Interfaces功能. 下 ...
- 【语义分割】large kernel matters中GCN模块的pytorch实现
GCN模块的实现比较简单,在giuhub上看到两种实现,轻微不同 实现一:https://github.com/ycszen/pytorch-segmentation/blob/master/gcn. ...