【问题标题】:Why does python keep saying the return statement is outside of the function? [closed]为什么python一直说return语句在函数之外? [关闭]
【发布时间】:2014-12-18 16:20:21
【问题描述】:
single_char = {}
for item in lines:
    for index in range(len(item)):
        if item[index - 1] in space:
            if item[index + 1] in space:
                if item[index] in alplist:
                    char = item[index]
                    single_char[char] = index
                    return single_char
                else:
                    continue
            else:
                continue
        else:
            continue​

这是我的函数,由于某种原因,Python 一直告诉我返回语句“return single_char”在函数之外。有人可以解释我如何解决这个问题。

【问题讨论】:

  • 因为你把缩进搞砸了。
  • 嗯,据我所知没有函数定义。

标签: python return


【解决方案1】:

没有功能。 return 语句应该始终在函数内部。 在python中使用def关键字定义函数。

示例:


def sum(a, b):
    return a+b

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-26
    • 2012-12-09
    • 2021-02-12
    • 1970-01-01
    • 2022-11-20
    • 2022-01-07
    • 2016-05-17
    • 1970-01-01
    相关资源
    最近更新 更多