input函数出现的问题(Python)
参考书<A Learner's Guide to Programming Using the Python Language>,写下如下的程序:
# coding=utf-8
# 以上是为了能在文中添加中文的注释 def save_transaction(price, credit_card, description):
file = open("transaction.txt", "a")
file.write("%s%07d%16s\n" % (credit_card, price, description))
file.close() items = ["DONUT","LATTE","FILTER","MUFFIN"]
prices = [1.50, 2.0, 1.80, 1.20]
running = True while running:
option = 1
for choice in items:
print(str(option) + "." + choice)
option = option + 1 print(str(option) + ".Quit") choice = int(input("Choose an option: "))
if option == choice:
running = False
else:
#用input的话,如果是以0开始的字符串的话,就会报错
credit_card = input("Credit card number: ")
while len(credit_card) != 16 :
credit_card = input("the length of credit card number must be equal to 16. Please input Credit card number again: ")
save_transaction(prices[choice-1]*100, credit_card, items[choice-1])
但是,在运行到第26行的时候,如果输入“0987648900804387”时,会出现如下的错误:
Traceback (most recent call last):
File "transaction.py", line 26, in <module>
credit_card = input("Credit card number: ")
File "<string>", line 1987648900804387
^
SyntaxError: invalid token
上网查了下,注意到一个细节:
input其实是通过raw_input来实现的,原理很简单,就下面一行代码:[参考 http://www.cnblogs.com/linlu11/archive/2009/11/25/1610830.html]
def input(prompt):
return (eval(raw_input(prompt)))
那么,简单写了测试函数:
>>> eval("")
83
>>> eval("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1987
^
SyntaxError: invalid token
>>> eval("0x9d")
157
>>> eval("0x9h")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
0x9h
^
SyntaxError: unexpected EOF while parsing
可见,在input函数中,调用eval时候,会根据字符串中的第一个或者前2个字符来判断字符串对应是8进制还是16进制的,
如果是0,就是按照8进制来处理:所以上面的输入是 0987648900804387 非法的,不是一个有效的8进制数;
如果是0x,就按照16进制来处理,所以0x9h也是非法的。
因此,原程序中的第26行到28行,可以换成:
credit_card = raw_input("Credit card number: ")
while len(credit_card) != 16 :
credit_card = raw_input("the length of credit card number must be equal to 16. Please input Credit card number again: ")
input函数出现的问题(Python)的更多相关文章
- Python中的print、input函数以及Python中交换两个变量解析
一.Python中的值交换操作 首先明确一点点,Python中的一切都是面向对象的,可以理解为Python的中一切都是对象. 我们知道Java也是面向对象的语言,但是在Java中定义一个值变量如下: ...
- python中子进程不支持input()函数输入
错误的源代码: import socketimport threadingimport multiprocessing# 创建socketserve_socket = socket.socket(so ...
- Python input() 函数
Python3.x 中 input() 函数接受一个标准输入数据,返回为 string 类型. Python2.x 中 input() 相等于 eval(raw_input(prompt)) ,用来获 ...
- 16.Python input()函数:获取用户输入的字符串
input() 函数用于向用户生成一条提示,然后获取用户输入的内容.由于 input() 函数总会将用户输入的内容放入字符串中,因此用户可以输入任何内容,input() 函数总是返回一个字符串. 例如 ...
- 【Python笔记】2020年7月30日练习【python用input函数输入一个列表】
练习课题链接:廖雪峰-Python教程-高级特性-迭代 学习记录: 1.Python当中类似于 三目运算符 的应用 2.Python用input函数输入一个列表 代码实例:对用户输入的一组数字转化成l ...
- Python手把手教程之用户输入input函数
函数input() 函数 input() 让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. 例如,下面的程序让用户输入一些文本,再将这些文本呈现给 ...
- python基础4 input()函数
input()函数 赋值输出: name=input('请求输入你喜欢的电影名:')print(name+'是我最喜欢的电影!') 输入:大话西游 输出:大话西游是我最喜欢的电影! print('那么 ...
- python通过input()函数输入的内容是什么类型
说明: 通过input()函数,可以从标准输入读取内容,那么读到的内容是什么类型呢. 通过type()函数可以进行判断,另外,通过input()函数的官方解释,从标准输入读取一个字符串.所以,应该是字 ...
- python学习笔记一: 《python3 input()函数》
一.在学习之前需要先了解: 1.Python3.x 中 input() 函数接受一个标准输入数据,返回为 string 类型,即把任何输入看作str. 2.input可以用作文本输入,如用户名,密码框 ...
随机推荐
- vue.js学习(第一课)
学习资料 来自台湾小凡! vue.js是javascript的一个库,只专注于UI层面,核心价值永远是 API的简洁. 第一课: 不支持IE8. 1.声明式渲染: el元素的简称 element : ...
- hTML5实现表单内的上传文件框,上传前预览图片,针刷新预览images
hTML5实现表单内的上传文件框,上传前预览图片,针刷新预览images, 本例子主要是使用HTML5 的File API,建立一個可存取到该file的url, 一个空的img标签,ID为img0,把 ...
- C# 获取当前域的路径值
做域认证的情况下, 要先获取域的path, 可以先用代码获取当前域的path. string pathCur = "LDAP://RootDSE"; DirectoryEntry ...
- Mybatis传入参数类型为Map
mybatis更新sql语句: <update id="publishT00_notice" parameterType="Map"> update ...
- AnjularJS系列3 —— 数据的双向绑定
第三篇,双向的数据绑定 数据绑定是AnguarJS的特性之一,避免书写大量的初始代码从而节约开发时间 数据绑定指令提供了你的Model投射到view的方法.这些投射可以无缝的,毫不影响的应用到web应 ...
- mysql 查询制定日期数据
//获取昨天更新数据的条件date_sub(current_date(), interval 1 day) = from_unixtime(senddate, '%Y-%m-%d') 注释:date_ ...
- php验证身份证号码的正确性
/********************php验证身份证号码是否正确函数*********************/function is_idcard( $id ) { $id = strto ...
- dos 批处学习笔记
dos 批处理@ 只显示命令结果echo 回显pause 暂停del 删除set 查看系统变量>nul 正确命令输入空洞2>nul 错误命令输入空洞dir 显示目录和文件&& ...
- Python之路【第二十三篇】爬虫
difference between urllib and urllib2 自己翻译的装逼必备 What is the difference between urllib and urllib2 mo ...
- GCD总结
//用block只有两种:同步执行/异步执行(参数1:队列;参数二:任务) dispatch_async(dispatch_get_global_queue(0, 0),^{ });//异步在新的线程 ...