• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python没有报错提示

python 搞java代码 3年前 (2022-05-21) 17次浏览 已收录 0个评论

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

def count_words(<a href="https://www.gaodaima.com/tag/filename" title="查看更多关于filename的文章" target="_blank">filename</a>):
    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.')

www#gaodaima.com来源gaodai#ma#com搞*!代#%^码$网搞代码

原因:

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

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

更多Python知识,请关注:搞代码网!!

来源:搞代码网:原文地址:https://www.gaodaima.com


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python没有报错提示
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址