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
巴扎黑巴扎黑2741 days ago533

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-18 10:28:44

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

    reply
    0
  • PHPz

    PHPz2017-04-18 10:28:44

    return needs to be used inside a function

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:28:44

    return can generally only be used in functions.

    reply
    0
  • Cancelreply