-1

我正在尝试在 Eclipse 中使用 PyDev 创建一个类,但是当我尝试作为 Python 运行时出现错误: TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'.

我尝试选择所有文本,然后在源菜单下选择“正确缩进”,但我在 Eclipse 中找不到该选项。

如何在 Python 3 中解决这个问题?

这是代码(从此处转录):

class Employee:
    empCount = 0

    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
        Employee.empCount += 1

    def displayCount(self):
        print("Total Employee %d") % Employee.empCount

    def displayEmployee(self):
        print ("Name:"), self.name, "Salary: ", self.salary

def main():
    emp1 = Employee("Zara", 2000)
    emp2 = Employee("Manni", 5000)
    emp1.displayEmployee()
    emp2.displayEmployee()
    print("Total Employee %d") % Employee.empCount

if __name__ == '__main__':
    main()
4

3 回答 3

3

No, indentation problems aren't your concern - the way you're printing out your information is.

In Python3, print() is now a function which returns None when done, then applying some operator to None, hence your exception.

I notice that you're attempting to do the old style of string formatting by using the % operator to specify when the formatting begins - the use of this in Python 3 is discouraged.

Here's how you get past it: use the curly-brace notation and call .format() with the parameters you want to format. It's positional, so multiple curly braces will format multiple arguments.

Example:

def displayEmployee(self):
    print ("Name: {}, Salary: {:d}".format(self.name, self.salary))

That's the most complex of your print statements; I feel that you can get the rest from here.

于 2013-07-24T04:45:10.333 回答
0

问题是您在输入所有参数之前关闭了打印功能,您应该执行以下操作: print("Total Employee %d" % Employee.empCount) <-

顺便说一句,在 python 3 中, print 有一个新的语法,它需要括号,所以:

print ("Name:"), self.name, "Salary: ", self.salary

你必须写

print ("Name:", self.name, "Salary: ", self.salary)

我希望它有效

于 2013-07-24T08:16:44.417 回答
-1

您没有缩进问题。

您使用三个空格来缩进而不是四个。这对 Python 来说不是问题,但 Eclipse 认为您应该使用四个并在其下放置波浪线。如果您无法让 Eclipse 解决此问题,只需使用四个空格即可。

于 2013-07-24T04:41:23.453 回答