您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页python没有报错提示

python没有报错提示

来源:化拓教育网

Python中没有报错提示的代码示例:

def count_words(filename):
    try:
        with open(filename) as f_obj:
            contents=f_obj.read()
    except FileNotFoundError:
        pass    
    else:
        words=contents.spilt()
        num_words=len(words)
        print("This file"+filename+'has about'+str(num_words)+'words.')
===========================================================================================
def count_words(filename):
    try:
        with open(filename) as f_obj:
            contents=f_obj.read()
    except FileNotFoundError:
        message='sorry,the file'+filename+'does not exits'
        print(message)
    else:
        words=contents.spilt()
        num_words=len(words)
        print("This file"+filename+'has about'+str(num_words)+'words.')

原因:

其中使用了 try-except-else 的代码块,except是try代码中错误时执行,而except中的代码是pass,也就是在try中代码错误时候直接pass,不给用户编写任何的建议和help。

相应报错的代码也写在了横线下面。

更多Python知识,请关注:!!

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务