如何摆脱python中的return关键字错误

时间:2018-09-28 09:15:22

标签: python

我是Python的新手。 我写了下面的代码来测试python中的方法。 但是,返回行将引发错误,不确定原因。 下面是代码和错误。

class Employee():

    def __init__(self, first ,last,email ,pay):
     self.first = first
     self.last = last
     self.email = email
     self.pay = pay

     def fullname(self):
    return '{} {}'.format(self.first. self.last)


emp1 = Employee("COREY","Schafer","COREY.Schafer@gmail.com",60000)
emp2 = Employee("rahul","ravi","rahul.ravi@emc.com","70000")

print(emp1.email)
print(emp2.email)

#print('{} {}'.format(emp1.first, emp1.last))

print(emp1.fullname())

错误:

    return '{} {}'.__format__(self.first. self.last)
    ^
IndentationError: expected an indented block

2 个答案:

答案 0 :(得分:1)

我相信这是您要尝试做的事情:

class Employee():
    def __init__(self, first ,last,email ,pay):
        self.first = first
        self.last = last
        self.email = email
        self.pay = pay
    def fullname(self):
        return '{} {}'.format(self.first. self.last)

在给定的代码中,缩进是有问题的。

答案 1 :(得分:0)

Python对缩进非常非常敏感。每个缩进级别的代码块都应该恰好有4个空格。

所以,这是错误的:

class Employee():
    def __init__(self, first ,last,email ,pay):
     self.first = first
     self.last = last
     self.email = email
     self.pay = pay

     def fullname(self):
    return '{} {}'.format(self.first. self.last)

这可以:

class Employee:
    def __init__(self, first, last, email, pay):
        self.first = first
        self.last = last
        self.email = email
        self.pay = pay

    def fullname(self):
        return '{} {}'.format(self.first, self.last)

编辑:您在返回时也出错。