原创 qq_23183809 最后发布于2020-03-14 15:04:15 阅读数 10 收藏
展开
how are you tom and mali dgsdf
---
file_name = input('input you want to print file:')
f= open(file_name)
print('the context is :')
for each_line in f:
print(each_line)
---
try: # 一旦遇到异常 后面的将不会被执行
sum=1+'1'
f = open('hello.txt')
print(f.read())
f.close()
except IOError as reason:
print('IOError error,\n the error reason is ' + str(reason))
except TypeError as reason:
print('TypeError error,\n the error reason is ' + str(reason))
------------
try: # 一旦遇到异常 后面的将不会被执行
f = open('hello.txt','w')
print(f.write('how are you tom and mali dgsdf'))
sum=1+'1'
except (OSError,TypeError):
print('IOError or TypeError error')
raise ZeroDivisionError
finally:
f.close()
----------
p34
def showmaxfactor(num):
count=num//2
while count>1:
if num%count == 0:
print('%d max factor is %d' %(num,count))
break
count-=1
else:
print('%d is sushu'%num)
num = int(input('pleas input a num:'))
showmaxfactor(num)
---------------------
'''
try:
int('123')
except ValueError as reason:
print('input error '+str(reason))
else:
print('ni 你input ok')
'''
----------------
'''
try:
f=open('ok.txt','w')
for each_line in f:
print(each_line)
except IOError as reason:
print('出错了:'+str(reason))
finally:
f.close()
'''
'''
try:
with open('ok.txt','w') as f:
for each_line in f:
print(each_line)
except IOError as reason:
print('input error :'+str(reason))
'''