我想通过printPerson使用x和y来打印,但是我无法解决此问题。当我尝试使用x.printPerson时,它指出未定义printPerson。怎么来的?
class Person:
def __init__ (self, name, age, strong, vehicle):
self.name = name
self.age = age
self.strong = strong
self.vehicle = vehicle
def printPerson(self):
print("Name:" + self.name)
print("Age:" + self.age)
print("Strong:" + self.strong)
print("Vehicle:" + self.vehicle)
x = Person("x", 20, ["Strong", "Strong", "Strong"], ["Car" , "Bike"])
y = Person("y",21, ["Weak", "Weak", "Super weak"], ["bus", "tractor"])
如何打印x输出如下:
>>> x.printPerson()
Name: x
Age: 20
Strong: "Strong, Strong, Strong"
Vehicle: "Car, Bike"
答案 0 :(得分:0)
尝试:
x.printPerson()
而不是:
x.printPerson
完整代码:
class Person(object):
def __init__ (self, name, age, strong, vehicle):
self.name = name
self.age = age
self.strong = strong
self.vehicle = vehicle
def printPerson(self):
print("Name:" + self.name)
print("Age:" + str(self.age))
print("Strong:" + ", ".join([val for val in self.strong]))
print("Vehicle:" + ", ".join([val for val in self.vehicle]))
x = Person("x", 20, ["Strong", "Strong", "Strong"], ["Car" , "Bike"])
x.printPerson()