“返回”外部功能

时间:2012-11-13 11:21:33

标签: python python-2.7

为什么这会给我错误'return' outside function

Result = ""
char = ""

    # Let char loop over each character in the input string

a = list(s)
for i in range (0, len(s)):

        # If this char is a letter:

    if (type(a[i])=='str'):

            # Set char_low to be char converted to lower case

        char_low = a.lower()
        print a

            # Note: the next 5 lines do not need to be changed -
            #  they will modify the character as necessary
    if char_low <= 'm':
        dist = 13
    else:
         dist = -13
    char = chr(ord(char) + dist)
        # Push char onto the end of the result

    result = result + char

    # Return the encrypted text

return result

2 个答案:

答案 0 :(得分:9)

好吧,因为回归是在外面的功能。

答案 1 :(得分:5)

return语句只能在函数定义中使用,例如:

def myfunc():
    result = 1
    return result

如果结果是退出代码,您可能要么使用sys.exit(result),要么更有可能将结果打印到控制台,在这种情况下只需使用print(result)