-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
4

2 回答 2

9

好吧,因为返回是外部函数。

于 2012-11-13T11:27:28.233 回答
5

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

def myfunc():
    result = 1
    return result

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

于 2012-11-13T11:38:13.840 回答