Home  >  Q&A  >  body text

python 为什么用return会报错啊?

try:
    r = requests.get('http://www.baidu.com',timeout=30)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    return r.text
except:
    return '我怎么知道又错了' 
  SyntaxError: 'return' outside function


小天小天2770 days ago1288

reply all(2)I'll reply

  • 数据分析师

    数据分析师2017-10-01 00:54:56

    python Why does it report an error when using return? -PHP Chinese website Q&A-python Why do I get an error when using return? -PHP Chinese website Q&A

    Let’s take a look and learn.

    reply
    0
  • 迷茫

    迷茫2017-03-20 09:37:28

    import requeststry:
        r = requests.get('http://www.baidu.com',timeout=30)
        r.raise_for_status()
        r.encoding = r.apparent_encodingexcept:
        print('我怎么知道又错了')
        
    print(r.text)return是在函数中返回


    reply
    0
  • Cancelreply