2014年1月2日13:47:36
错误:invalid syntax。
在学习‘引发异常’中遇到。
解决:修改为:
即可。
因 错误写法为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') 即可。
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导致
以下是程序本身的运行结果以及外部引用的运行结果
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~华丽分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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_)
应该这么写:print (printMax.__doc__)。注意doc左右的下划线,你两边分别写了1个,应该分别写2个
最后一行代码改为如下就OK了。print(printMax.__doc__)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~华丽分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2013年12月9日15:24:55
错误: ‘raw_input ’ is not defined.
出现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
大囧,我看了看教程,又看了看我的,明明木有错儿啊,后来查了查发现:python v3.0以后的版本中将v2.x版本的print 改为了print(); 所以下面就成功了~~ (教材太落后啦~)