"返回"外部功能

BEE*_*BEE -6 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
Run Code Online (Sandbox Code Playgroud)

not*_*row 9

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

  • +1呵呵.呵呵.嘿嘿. (2认同)
  • 这是OP应得的答案,但不是他现在需要的答案...... (2认同)

aqu*_*tae 5

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

def myfunc():
    result = 1
    return result
Run Code Online (Sandbox Code Playgroud)

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