python版本问题_python版本导致的错误 - 安然美的个人空间 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...

2014年1月2日13:47:36

错误:invalid syntax。

6597545855193226724.jpg

在学习‘引发异常’中遇到。

解决:修改为:

2970405429327586988.jpg即可。

因 错误写法为2.6版本以前用法~ 2.6版本后改为:except ShortInputException as x:

正确源代码:# Filename: raising.py

classShortInputException(Exception):

'''A user-defined exceptiong class.'''

def__init__(self,length,atleast):

Exception.__init__(self)

self.length=length

self.atleast=atleast

try:

s=input('Enter something --> ')

iflen(s)<3:

raiseShortInputException(len(s),3)

# Other work can continue as usual here

exceptEOFError:

print('\nWhy did you do an EOF on me?')

exceptShortInputExceptionasx:

print('ShortInputException: The input was of length %d, \

was expecting at least %d'%(x.length,x.atleast))

else:

print('No exception was raised.')

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~华丽分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2013年12月30日17:18:56

错误:ImportError: No module named 'cPickle'

解决:python3以后cPickle 和pickle 模块合并,所以直接引入 pickle

错误:p.dump(shoplist, f)TypeError: must be str, not bytes

解决:python3以后如果要用存储器,那么读写文件都要用‘rb’和'wb'模式。将

f =file(shoplistfile,'w')改为f = open(shoplistfile, 'wb')

f =file(shoplistfile) 改为f = open(shoplistfile, 'rb')

正确代码如下

# Filename: pickling.py

# import cPickle as p cPickle is not exist

importpickleasp

shoplistfile='shoplist.data'

# the name of the file where we will store the object

shoplist=['apple','mango','carrot']

# Write to the file

f=open(shoplistfile,'wb')

p.dump(shoplist,f)# dump the object to a file

f.close

delshoplist# remove the shoplist

# Read back from the storage

f=open(shoplistfile,'rb')

storedlist=p.load(f)

print(storedlist)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~华丽分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2013年12月30日15:59:14

错误:NameError: name 'file' is not defined

解决:没有 file() 方法,将:f =file('poem.txt','w')改为:f = open('poem.txt', 'w')

f =file('poem.txt')改为f = open('poem.txt') 即可。

4929189792257118958.jpg

poem='''\

Programming is fun

When the work is done

if you wanna make your work also fun:

'''

f=open('poem.txt','w')# open for 'w'riting

f.write(poem)# write text to file

f.close()# close the file

f=open('poem.txt')

# if no mode is specified, 'r'ead mode is assumed by default

whileTrue:

line=f.readline()

iflen(line)==0:# Zero length indicates EOF

break

print(line),

# Notice comma to avoid automatic newline added by Python

f.close()# close the file

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~华丽分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2013年12月11日16:31:23

错误:AttributeError: 'module' object has no attribute '__path__'

错误:ImportError: No module named 'using_name.py'; using_name is not a package

原因,在python目录下建立了test目录保存所写小程序。但是环境变量path中只写到了python根目录。

解决:增加path环境变量到目录 test目录下。并且在引入时应该写为:import use_name,错误的写成了:import use_name.py导致

3131690590983075936.jpg 以下是程序本身的运行结果以及外部引用的运行结果

6597649209286222279.jpg

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~华丽分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2013年12月10日15:28:41

错误:'function' object has no attribute '_doc_'

源程序# Filename: func_doc.pydefprintMax(x,y):'''Prints the maximum of two numbers.The two values must be integers.'''x=int(x)y=int(y)ifx>y:print(x,'is maximum')else:print(y,'is maximum')printMax(3,5)print(printMax._doc_)

6597976863751297276.jpg

应该这么写:print (printMax.__doc__)。注意doc左右的下划线,你两边分别写了1个,应该分别写2个

最后一行代码改为如下就OK了。print(printMax.__doc__)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~华丽分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2013年12月9日15:24:55

错误: ‘raw_input ’ is not defined.

4819133076363204594.jpg

出现name 'raw_input' is not defined错误,原因是版本3.0 以后去掉了raw_input 函数,改用input。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~华丽分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2013年12月9日11:51:47

今儿想了想,最近不忙,学点什么吧,python吧,跟兔子八卦了一会,他也建议这个,果断找个教程,开始下载安装。第一个么,当然 hello world,不过,刚来就挫败,SyntaxError: invalid syntax

错误:SyntaxError: invalid syntax

3378825620535228678.jpg 大囧,我看了看教程,又看了看我的,明明木有错儿啊,后来查了查发现:python v3.0以后的版本中将v2.x版本的print 改为了print(); 所以下面就成功了~~ (教材太落后啦~)

1833527998393408461.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值