小编给大家分享一下Python中没有报错提示的代码示例,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨方法吧!

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

defcount_words(filename):try:withopen(filename)asf_obj:contents=f_obj.read()exceptFileNotFoundError:passelse:words=contents.spilt()num_words=len(words)print("Thisfile"+filename+'hasabout'+str(num_words)+'words.')===========================================================================================defcount_words(filename):try:withopen(filename)asf_obj:contents=f_obj.read()exceptFileNotFoundError:message='sorry,thefile'+filename+'doesnotexits'print(message)else:words=contents.spilt()num_words=len(words)print("Thisfile"+filename+'hasabout'+str(num_words)+'words.')

原因:

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

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

看完了这篇文章,相信你对Python中没有报错提示的代码示例有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!