“return”外部函数

2024-04-20 14:24:02 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么这会给我错误'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

Tags: thetoinreturnifdist错误function
2条回答

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

def myfunc():
    result = 1
    return result

如果结果是一个退出代码,您可能希望使用sys.exit(result),或者更可能的是,将结果打印到控制台,在这种情况下,只需使用print(result)。在

好吧,因为回报是函数之外的。在

相关问题 更多 >