python31-34


原创 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))
'''
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值