函数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. XXX培训机构Python自动化教程目录

  2. CF1137E. Train Car Selection(可删堆)

    题面 三个操作 1.在当前数列最左端加入\(k\)个初始为\(0\)的数 2.在当前数列最右端加入\(k\)个初始为\(0\)的数 3.将当前数列从左到右第\(i\)个数加上\(b+(i-1)k(b& ...

  3. Centos查看端口占用令

    Centos查看端口占用情况命令,比如查看80端口占用情况使用如下命令: lsof -i tcp:80 列出所有端口 netstat -ntlp 1.开启端口(以80端口为例) 方法一: /sbin/ ...

  4. 5.EM

  5. 使用C语言封装数组,动态实现增删改查

    myArray.h : #pragma once //包含的时候只包含一次 #include <stdio.h> #include <stdlib.h> #include &l ...

  6. 在FC中如何获取fcdot文件

    在FlexiCapture中一些客户在问如何获取.fcdot文件(在测试序列号下或者没有测试模板的情况下) 第一步: 1.查看License Manager查看是否找到序列号 首先我们在开始菜单里面打 ...

  7. 什么是redis?Reids的特点是什么?Redis支持的数据类型有哪些?

    首先,分布式缓存框架 可以 看成是nosql的一种 (1)什么是redis? redis 是一个基于内存的高性能key-value数据库. (有空再补充,有理解错误或不足欢迎指正) (2)Reids的 ...

  8. struts2学习笔记(六)—— 拦截器

    一.拦截器概述 拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前进行拦截,然后在之前或之后加入某些操作.拦截器是AOP的一种实现策略. 在We ...

  9. Flask 知识点

    flask run时候端口占用的问题 终端 lsof -i:5000 kill <端口号> 强制删除 kill -s 9 <端口号> 给网页标题添加icon {% block ...

  10. Rebranding(模拟+思维)

    The name of one small but proud corporation consists of n lowercase English letters. The Corporation ...