函数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

最新文章

  1. [Google Guava]学习--新集合类型Multimap
  2. 【codevs1907】 方格取数 3
  3. 软件设计之UML&mdash;UML的构成[上]
  4. .hpp文件
  5. IAR产生可烧录的镜像文件
  6. JDBC操作数据库的学习(2)
  7. 取一种类型里面的产品销售前3甲的数据Sql
  8. 机器人行业中我们常说的roll、yaw、pitch是什么?
  9. 牛客小白月赛13 小A的柱状图(单调栈)
  10. 笔记:Zygote和SystemServer进程启动过程
  11. 数据库日志redo和undo
  12. 数据库基础SQL知识面试题二
  13. jmeter基本使用
  14. Luogu 4234 最小差值生成树 - LCT 维护链信息
  15. 【Unity】角色受伤后的闪烁(blink/flash)效果
  16. 重构 MVC; 代码分享工具(重构,改进,打分)
  17. $NOIp$前的日常
  18. 多示例学习 multiple instance learning (MIL)
  19. OpenCV 颜色空间转换参数CV_BGR2GRAY改变
  20. NRF24L01

热门文章

  1. Android app如何加密?
  2. rsync实时备份备份服务搭建和使用指南
  3. dubbo服务治理中间件,zookeeper注册中心 安装配置
  4. 基于redis的分布式锁的分析与实践
  5. 【大数据系统架构师】1.2 大数据基础Hadoop 2.X
  6. leecode刷题(7)-- 加一
  7. 洛谷P4016 负载平衡问题(费用流)
  8. SDUT OJ 数据结构上机测试1:顺序表的应用
  9. Android---16进制与字节数组
  10. Java getMethod类型参数