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可以用作文本输入,如用户名,密码框 ...
随机推荐
- shell及脚本3——正则表达式
一.正则表达式 1.1. 什么是正则表达式 正则表达式是处理字符串的方法,以行为单位,通过一些特殊符号的辅助,让用户可以轻易进行查找.删除.替换某特定字符串的操作. 1.2. 正则表达式与通配符的区别 ...
- 点击div 跳转并通过URL传参
点击div前要先给div绑定要传的参数: //给panel绑定自定义属性,方便在跳转时传带参数,键/值对排列 panel.attr("user_age",user_age); pa ...
- Dockerfile初探
git上的asp.net samples工程已经写好了docker file,内容是如下 //任何dockersfile都要以FORM开头,约定是用大写. FROM microsoft/aspne ...
- Oracle11g字符集AL32UTF8修改为ZHS16GBK详解
此问题发生在数据库迁移过程中.源数据库:自己笔记本上win7 64位系统的oracle11g个人版,字符集ZHS16GBK :目标数据库,HP的sqlserver2008 系统 64位数据库服务器,字 ...
- 建模前的数据清洗/ETL(python)
1. 读取数据 data= open('e:/java_ws/scalademo/data/sample_naive_bayes_data.txt' , 'r') 2. 把数据随机分割为trainin ...
- 附加属性出现Failed to assign to property的问题
找了半天资料,最后发现把保护附加属性的类加上public就行了
- 【BZOJ-1127】KUP 悬线法 + 贪心
1127: [POI2008]KUP Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 317 Solved: 11 ...
- bzoj 1711 [Usaco2007 Open]Dining吃饭&&poj 3281 Dining
最大流. 这东西好像叫三分图匹配. 源点向每个食物点连一条容量为1的边. 每个饮料点向汇点连一条容量为1的边. 将每个牛点拆点,食物点向喜欢它的牛的入点连一条容量为1的边,牛的出点向它喜欢的饮料点连一 ...
- adb工具获取appPackage和appActivity
1,手机连接电脑,打开手机调试功能,并运行待测试APP,终端执行: adb shell 2,接着,执行: dumpsys window windows | grep -E 'mFocusedApp' ...
- Day6-python基础之模块
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...