参考书<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)的更多相关文章

  1. Python中的print、input函数以及Python中交换两个变量解析

    一.Python中的值交换操作 首先明确一点点,Python中的一切都是面向对象的,可以理解为Python的中一切都是对象. 我们知道Java也是面向对象的语言,但是在Java中定义一个值变量如下: ...

  2. python中子进程不支持input()函数输入

    错误的源代码: import socketimport threadingimport multiprocessing# 创建socketserve_socket = socket.socket(so ...

  3. Python input() 函数

    Python3.x 中 input() 函数接受一个标准输入数据,返回为 string 类型. Python2.x 中 input() 相等于 eval(raw_input(prompt)) ,用来获 ...

  4. 16.Python input()函数:获取用户输入的字符串

    input() 函数用于向用户生成一条提示,然后获取用户输入的内容.由于 input() 函数总会将用户输入的内容放入字符串中,因此用户可以输入任何内容,input() 函数总是返回一个字符串. 例如 ...

  5. 【Python笔记】2020年7月30日练习【python用input函数输入一个列表】

    练习课题链接:廖雪峰-Python教程-高级特性-迭代 学习记录: 1.Python当中类似于 三目运算符 的应用 2.Python用input函数输入一个列表 代码实例:对用户输入的一组数字转化成l ...

  6. Python手把手教程之用户输入input函数

    函数input() 函数 input() 让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. 例如,下面的程序让用户输入一些文本,再将这些文本呈现给 ...

  7. python基础4 input()函数

    input()函数 赋值输出: name=input('请求输入你喜欢的电影名:')print(name+'是我最喜欢的电影!') 输入:大话西游 输出:大话西游是我最喜欢的电影! print('那么 ...

  8. python通过input()函数输入的内容是什么类型

    说明: 通过input()函数,可以从标准输入读取内容,那么读到的内容是什么类型呢. 通过type()函数可以进行判断,另外,通过input()函数的官方解释,从标准输入读取一个字符串.所以,应该是字 ...

  9. python学习笔记一: 《python3 input()函数》

    一.在学习之前需要先了解: 1.Python3.x 中 input() 函数接受一个标准输入数据,返回为 string 类型,即把任何输入看作str. 2.input可以用作文本输入,如用户名,密码框 ...

随机推荐

  1. html种种

    DIV+CSS如何让文字垂直居中?--https://zhidao.baidu.com/question/69214815.html

  2. cmd常用命令

    2016.12.18 0:07 (持续更新) cd 目录名:打开文件目录, cd .. 返回上一目录 cd ... 返回上上级目录 cd \ 返回根目录 cls 清除当前cmd页面所有的记录 md 名 ...

  3. ISO

  4. Bootstrap JavaScript插件

      在bs3.X中,提供了12种JavaScript插件,分别是:动画过渡(Transition).模态弹窗(Modal).下拉菜单(Dropdown).滚动侦测(Scrollspy).选项卡(Tab ...

  5. Android基础测试题(四)

    看了前两道题大家有没有发现,测试题少了(一),大家猜猜测试题(一)是什么? Android基础测试题(四): 需求: 建一个方法,格式化输出2016-11-14 10:15:26格式的当前时间,然后截 ...

  6. 【poj3071】 Football

    http://poj.org/problem?id=3071 (题目链接) 题意 ${2^n}$个队伍打淘汰赛,输的被淘汰.第1个队打第2个队,第3个队打第4个队······给出第i个队伍打赢第j个队 ...

  7. 练习:python 操作Mysql 实现登录验证 用户权限管理

    python 操作Mysql 实现登录验证 用户权限管理

  8. Web系统大规模并发——电商秒杀与抢购

    电商的秒杀和抢购,对我们来说,都不是一个陌生的东西.然而,从技术的角度来说,这对于Web系统是一个巨大的考验.当一个Web系统,在一秒钟内收到数以万计甚至更多请求时,系统的优化和稳定至关重要.这次我们 ...

  9. iBatis.net 循环iterate,没有foreach

    3.9.4. Iterate Element This tag will iterate over a collection and repeat the body content for each ...

  10. JS中的动态表格

    写法一:(有点啰嗦) //--------------XML DOM--------------------------------------function addTR(){ //1.取三个框的值 ...