您现在的位置是:课程教程文章

python没有报错提示

2023-12-18 18:01课程教程文章 人已围观

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知识,请关注:Python自学网!!

课程教程:python没有报错提示

上一篇:python能实现并行吗

下一篇:没有了

站点信息

  • 文章统计篇文章