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