python版本导致的错误
# 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.')
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)
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:
use Python!
'''
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
以下是程序本身的运行结果以及外部引用的运行结果
# Filename: func_doc.py
defprintMax(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__)
TAG: