【问题标题】:Where to put return in python在 python 中将 return 放在哪里
【发布时间】:2021-04-11 23:12:41
【问题描述】:
x = [1,2,3,4,5,6,7,8,9,0]
for element in x:
return element

给出 IndentationError: 期望一个缩进块

x = [1,2,3,4,5,6,7,8,9,0]
for element in x:
     return element

给出错误>>> SyntaxError: 'return' outside function

【问题讨论】:

    标签: python-3.x return


    【解决方案1】:

    一个return语句用于结束函数调用的执行:

    x = [1,2,3,4,5,6,7,8,9,0]
    def fun():
    for element in x:
         return element
    

    您还可以从一个函数中返回多个值。

    return a,b,c
    

    【讨论】:

    • 非常感谢 :-)
    【解决方案2】:

    您需要在函数中定义 this 并调用该函数以获得返回值。像这样 -

    x = [1,2,3,4,5,6,7,8,9,0]
    def find_element():
    for element in x:
         return element
    a = find_element()
    print (a)
    

    代码 - https://ide.geeksforgeeks.org/MGO9MBWFyz

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2014-04-02
      • 1970-01-01
      相关资源
      最近更新 更多