函数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. go语言 defer 你不知道的秘密!

    go 语言的defer功能强大,对于资源管理非常方便,但是如果没用好,也会有陷阱哦.我们先来看几个例子. 例一: defer 是先进后出 这个很自然,后面的语句会依赖前面的资源,因此如果先前面的资源先 ...

  2. 《Think in Java》

    chapter 1 对象导论 面向对象程序设计(Object-oriented Programming ,OOP) chapter 2 一切都是对象 字段和方法 若类的某个成员变量是基本数据类型,即是 ...

  3. [原创]用WinRAR实现VC源代码快速压缩创建

    [原创]用WinRAR实现VC源代码快速压缩创建 by edata @ cnblogs.com/edata 2017-5-8 22:31:57 我们有的时候需要对vc项目压缩打包,因为vc项目的无关文 ...

  4. underscore里面的debounce与throttle

    throttle 策略的电梯.保证如果电梯第一个人进来后,15秒后准时运送一次,不等待.如果没有人,则待机. debounce 策略的电梯.如果电梯里有人进来,等待15秒.如果又人进来,15秒等待重新 ...

  5. SDUT OJ 数据结构实验之二叉树五:层序遍历

    数据结构实验之二叉树五:层序遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  6. Python博文列表

    手把手|100行Python代码自动抢火车票!(包教包会):https://zhuanlan.zhihu.com/p/32928355 最全中华古诗词数据库, :https://github.com/ ...

  7. 使用带参数的SQL语句向数据库中插入空值

    private void button1_Click(object sender, EventArgs e) { string name = textBox1.Text; int age = Conv ...

  8. Python学习【第五篇】:面向对象及相关

    面向对象基础 基础内容介绍详见一下两篇博文: 面向对象初级篇 面向对象进阶篇 其他相关 一.isinstance(obj, cls) 检查是否obj是否是类 cls 的对象 1 2 3 4 5 6 c ...

  9. String Reduction问题分析

    问题描述: Given a string consisting of a,b and c's, we can perform the following operation: Take any two ...

  10. fiddler抓安卓

    1.tools connections  左 allow remote computersconnect  选中 2.配置模拟器 wifi 长按 修改网络 ip电脑ip 端口8888 ps:修改完不要 ...