python 中的input
渣渣之路。
一、 在python编程初学者指南中的第六章、使用参数和返回值的例子中:
# -*- coding: utf-8 -*- def display(message):
print message def give_me_five():
five = 5
return five def ask_yes_no(question):
"""
Ask a yes or no questions.
"""
response = None
while response not in ('y', 'n'):
response = input(question).lower()
return response display("here is a message for you\n")
number = give_me_five()
print "Here's what I got from give_me_five():", number
answer = ask_yes_no("\nPlease enter 'y' or 'n': ")
print "Thank you for entering:", answer
发现自己在pycharm下输入的:y会报错
Please enter 'y' or 'n': y
Traceback (most recent call last):
File "E:/Project/actneed411/furion/static/js/template/testa.py", line 25, in <module>
answer = ask_yes_no("\nPlease enter 'y' or 'n': ")
File "E:/Project/actneed411/furion/static/js/template/testa.py", line 19, in ask_yes_no
response = input(question).lower()
File "<string>", line 1, in <module>
NameError: name 'y' is not defined
但是,输入:'y'或者"y"却是对的:
here is a message for you
Here's what I got from give_me_five(): 5
Please enter 'y' or 'n': 'y' "y"
Thank you for entering: y
二、探究python中的input【1】
由【1】中的文档中,python2.7中输入函数有两种:
1、raw_input():返回的是字符串--string类型,即输入:1+2,返回显示的是:"1+2"
2、input():返回的是数值类型,int,float等,即输入:1+2,返回显示的是:3
而在python3中输入只有一种:
input():返回的是字符串--string类型,没有数值类型了相当于原来的raw_input()
【2】以前有分raw_input和input, raw_input读什么东西都是string, input会解析数据,
版本3合并了raw_input和input, 只能读到string了, 原先的可解析版本不安全,
如果要读到数值,使用类型转换:
a = int(input("a="))
恰好数中使用的是python是python3,这样就能解释通上边的问题了。
------------420 三--
参考链接:【1】、python输入函数input 2-----
【2】、python3中的input raw_input()变成了input()
python 中的input的更多相关文章
- Python中的input你真会吗?
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:一米阳光里的晴天娃娃 python中的input()方法是在控制台可 ...
- python中的input,print
此用例在python3.3.5中测试通过: 输入:在python中输入是使用input,下面示例代码表示把输入的值存入变量s中,并输入s 在这里提醒一下:使用input获取的值都是string类型
- python 中的input()和raw_input()功能与使用区别
在python中raw_input()和input()都是提示并获取用户输入的函数,然后将用户的输入数据存入变量中.但二者在处理返回数据类型上有差别. input()函数是raw_intput()和e ...
- Python中raw_input() & input() 的功能对比
raw_input的功能是方便的从控制台读入数据. input与raw_input都是Python的内建函数,实现与用户的交互,但是功能不同. 一.raw_input 下面介绍让raw_input的 ...
- Python中变量的基本使用
变量的基本使用 程序就是用来处理数据的,而变量就是用来存储数据的 目标 变量定义 变量的类型 变量的命名 01. 变量定义 在 Python 中,每个变量 在使用前都必须赋值,变量 赋值以后 该变量 ...
- python中raw_input() 与 input()
参考网址:http://www.cnblogs.com/way_testlife/archive/2011/03/29/1999283.html 在python中如何接收一个输入的字符串. 举个例子: ...
- python中input()和raw_input()的区别
两者均是python的内置函数,通过读取控制台的输入与用户实现交互.raw_input:将所有输入作为字符串看待,不管用户输入什么类型的都会转变成字符串. raw的 ...
- python中print和input的底层实现
print print的底层通过sys.stdout.write() 实现 import sys print('hello') print('world') print(520) sys.stdout ...
- Python中的print、input函数以及Python中交换两个变量解析
一.Python中的值交换操作 首先明确一点点,Python中的一切都是面向对象的,可以理解为Python的中一切都是对象. 我们知道Java也是面向对象的语言,但是在Java中定义一个值变量如下: ...
随机推荐
- jwt refresh token
$app->post('auth/refresh-token', ['middleware' => 'jwt.refresh', function() { try { $old_token ...
- wp插件
- Oracle SQL基本操作
Oracle数据库基本操作 1.概述 Oracle数据库客户端一般需要安装在服务器上,可以在服务器端操作,一般我们可以用sql developer工具远程连接到数据库,先行建立数据库,然后对表进行增删 ...
- ubuntu14.04安装注意事项
1.虚拟机: 选择桥接2.设置静态IP sudo vi /etc/network/interfaces 修改为: # The loopback network interface auto lo if ...
- 微软良心之作——Visual Studio Code 开源免费跨平台代码编辑器
微软良心之作——Visual Studio Code 开源免费跨平台代码编辑器 在 Build 2015 大会上,微软除了发布了 Microsoft Edge 浏览器和新的 Windows 10 预览 ...
- mac 升级vim
首先,要下载vim的源代码.Vim source archives : vim online,下载7.4的新建一个目录用于安装vim 7.4:sudo mkdir /usr/local进入源代码的sr ...
- Dijkstar算法的数学原理
Dijkstar算法是荷兰数学家迪克斯屈拉(or迪杰斯特拉?)在1959年发现的一个算法.是现有的几个求带权图中两个顶点之间最短通路的算法之一.算是一个相当经典的算法了. 迪克斯屈拉算法应用于无向连通 ...
- 简介AngularJS中使用factory和service的方法
AngularJS支持使用服务的体系结构“关注点分离”的概念.服务是JavaScript函数,并负责只做一个特定的任务.这也使得他们即维护和测试的单独实体.控制器,过滤器可以调用它们作为需求的基础.服 ...
- Hibernate 一对多 保存和修改数据
Student和Sclass表,Student外键cid是Sclass的cid create table sclass( cid ) primary key, cname ) )go create t ...
- Why does pthread_cond_signal not work?【转】
转自:http://stackoverflow.com/questions/16819169/why-does-pthread-cond-signal-not-work# 0 down vote fa ...