函数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:会结束本层循环,不再运行循环中余下的代码;
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循环的更多相关文章

  1. 《Python编程从入门到实践》第三章_列表简介

    什么是列表呢? 官方说明就是由一些列按特点顺序排列的元素组成.其实可以看出很多个字符串的有序组合吧,里面的内容可以随时的删除,增加,修改. 下面这个就是一个列表,python打印列表的时候会将中括号和 ...

  2. 《python编程从入门到实践》第七章笔记

    用户输入和while循环 1.函数input():让程序停止运行,等待用户输入一些文本.接受一个参数,既即要向用户显示的提示或说明. 2.将数值输入用于计算和比较前,务必将其转换为数值表示. 3.fo ...

  3. 《python编程从入门到实践》第六章笔记

    1.字典 字典:一系列键-值对,每一个键都与每一个值相关联.与键相关联的值可以是数字.字符串.列表和字典. 最简单的字典只有一个键值对. eg: alien = {'color':'green','p ...

  4. 《Python编程从入门到实践》_第十章_文件和异常

    读取整个文件 文件pi_digits.txt #文件pi_digits.txt 3.1415926535 8979323846 2643383279 下面的程序打开并读取整个文件,再将其内容显示到屏幕 ...

  5. 《python编程从入门到实践》读书实践笔记(一)

    本文是<python编程从入门到实践>读书实践笔记1~10章的内容,主要包含安装.基础类型.函数.类.文件读写及异常的内容. 1 起步 1.1 搭建环境 1.1.1 Python 版本选择 ...

  6. Python编程从入门到实践笔记——异常和存储数据

    Python编程从入门到实践笔记——异常和存储数据 #coding=gbk #Python编程从入门到实践笔记——异常和存储数据 #10.3异常 #Python使用被称为异常的特殊对象来管理程序执行期 ...

  7. Python编程从入门到实践笔记——文件

    Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...

  8. Python编程从入门到实践笔记——类

    Python编程从入门到实践笔记——类 #coding=gbk #Python编程从入门到实践笔记——类 #9.1创建和使用类 #1.创建Dog类 class Dog():#类名首字母大写 " ...

  9. Python编程从入门到实践笔记——函数

    Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...

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

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

随机推荐

  1. SQL Server 根据关键字和结束符提取字符串子串

    /* @info-待截取的字符串 @indexStr-截取子串的起始字符串 @split-截取子串的结束符号 列入依次传入 胸片:正常.心电图:异常,需要注意.血常规检查:正常. 心电图 '.' 返回 ...

  2. golang subprocess tests

    golang Subprocess tests Sometimes you need to test the behavior of a process, not just a function. f ...

  3. Ubuntu chmod 命令可以用来修改文件或文件夹的读写权限

    chmod 命令有两种使用方式 —————————————————————————— (1)chmod [ u / g / o / a ] [ + / - / = ] [ r / w / x ] fi ...

  4. C# LINQ(7)

    大部分的LINQ的关键字都说了,最后说一下排序吧. LINQ的是查询的利器. 那么查询就会有排序. 所有LINQ提供了两种简单的排序.倒序和默认排序. 关键字是: orderby ascending ...

  5. Spring容器管理对象和new对象

    问题:一个业务类交给spring管理,并自动注入了其他业务类作为属性,方法中通过全局属性调用其他业务类的方法.当该业务类是通过new获取的情况下,该实例的属性会是null(不存在依赖注入实例),调用方 ...

  6. Ext中setValue和setRawValue

    Ext.getCmp('modifyStatus').setValue(record.get('status').trim()); Ext.getCmp('modifyStatus').setRawV ...

  7. Python的特殊属性和魔法函数

    python中有很多以下划线开头和结尾的特殊属性和魔法函数,它们有着很重要的作用. 1.__doc__:说明性文档和信息,python自建,不需要我们定义. # -*- coding:utf- -*- ...

  8. Linux环境下jdk、tomcat、redis安装及配置

    环境说明: linux : centOS jdk : 1.8 tomcat : 8.5 redis : 4.0 jdk安装配置 1.下载jdk(这里下载的是jdk-8u181-linux-x64.ta ...

  9. scrapy 调试功能

    在使用 scrapy 来爬取网页的时候,我们难免会使用到调试功能,下面介绍两种调试方法: 1.终端使用 scrapy shell exampleurl exampleurl 为你要爬取网站的 url ...

  10. Unity---动画系统学习(3)---使用状态机来实现走、跑、转弯等的动画切换

    1. 初始设置 用动画学习笔记(2)中方法,把动画全都切割好. 拖进状态机并设置箭头.并设置具体箭头触发的事件. 在状态机左侧中添加参数,Float和Int类型参数只能从-1~1之间变化 Float: ...