错误:在函数外“返回" [英] Error: 'return' outside of function

查看:27
本文介绍了错误:在函数外“返回"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:函数外的'return'

Error: 'return' outside of function

为什么??

 def get_all_students(self):
    database = open(self.database_path, 'r')
    list_of_students = list()
    for idx, l in enumerate(database.readlines()):
        params = list()
        params.append(idx)
        params += l.split(self.data_delimiter)
        student = Student(*self.item_un_escape(params))
        list_of_students.append(student)
    return list_of_students

我会发布课程的其余部分,但我很确定它的缩进是正确的.似乎无法找到错误,我敢打赌这可能是愚蠢的.抱歉,第一次用 python 编码,但这个错误已经让我烦了一个小时左右!哈哈

I'll post the rest of the class, I'm pretty sure it's correctly indented though. Can't seem to find the error, I can bet it's probably something silly. Apologies, first time coding in python but this error has been annoying me for like an hour or so! haha

Edit2:它在 params.append(idx) 上抛出一个错误,说出现了意外的缩进?

It's throwing an error on params.append(idx) saying unexpected indentation?

from student import Student

database_path = 'C:/Users/Alan/Desktop/flask/flask/app/database'


class Database(object):
    data_delimiter = ','

    @staticmethod
    def escape_new_lines(value):
        if type(value) == str:
            value = value.replace('\n', '@n-nl@')
            value = value.replace('\r', '@r-nl@')
        return value

    @staticmethod
    def un_escape_new_lines(value):
        if type(value) == str:
            value = value.replace('@n-nl@', '\n')
            value = value.replace('@r-nl@', '\r')
        return value

    def __init__(self, database_path=database_path):
        self.database_path = database_path

    def get_all_students(self):
        database = open(self.database_path, 'r')
        list_of_students = list()
        for idx, l in enumerate(database.readlines()):
            params = list()
            params.append(idx)
            params += l.split(self.data_delimiter)
            student = Student(*self.item_un_escape(params))
            list_of_students.append(student)
        return list_of_students

推荐答案

您发布的代码很好.您可能的错误是由于在 return 语句之前缺少缩进. 正如@Anonymous 在评论中指出的那样,另一种可能性是您将制表符缩进与空格缩进混合使用.检查您的缩进.

The code you have posted is fine. Your likely error is from missing an indent before a return statement. As @Anonymous has noted in the comments, another possibility as that you're mixing tab indents with space indents. Check your indentations.

这篇关于错误:在函数外“返回"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆