python中return关键字错误的消除

2024-03-28 11:14:42 发布

您现在位置: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

Tags: 代码selfformatreturnemaildef错误employee
2条回答

我相信这就是你想要做的:

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)

在给定的代码中,缩进是有问题的。你知道吗

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)

编辑:你也有一个错误作为回报。你知道吗

相关问题 更多 >