繁体   English   中英

Python中“函数外返回”语法错误的原因?

[英]Cause of “return outside of function” syntax error in Python?

我的代码有问题;

'return' outside function (<module1>, line 4)   <module1>   4

本书中的代码称为学习python困难的方法 ; “练习25:更多练习”

def break_words(stuff):
    """This function will break up words for us."""
words = stuff.split(' ')
return words


def sort_words(words):
    """Sorts the words."""
    return storred(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.php(0)
    print word

def sort_last_word(words):
    """prints the last word after popping it off."""
    word = words.pop(-1)
    print word

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence. """
    Words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prinits the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)

任何解决方案

在此函数中, return没有正确缩进。

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

您的缩进已关闭,请尝试以下操作:

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

与某些其他语言不同,缩进是Python的关键组成部分-因此,确保代码格式正确很重要。

缩进在python中很重要。 根据您粘贴的内容,这里的最后两行不属于break_words的范围。

def break_words(stuff):
    """This function will break up words for us."""
words = stuff.split(' ')
return words

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM