蓝大伟 发表于 2020-8-12 17:03:20

新手问题求助

class Dog:
    def __init__(self,name):
      self.name = name
    def game(self):
      print("狗自己玩")
class DogKing(Dog):
    def game(self):
      print("上天玩")
class Person:
    def __init__(self,name):
      self.name = name
    def gamewdog(self,dog):
      print("%s和%s快乐的玩耍"(self.name,dog.name))
      dog.game()
wangcai = DogKing("wangcai")
xiaoming = Person("小明")
xiaoming.gamewdog()


SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
print("%s和%s快乐的玩耍"(self.name,dog.name))
Traceback (most recent call last):
File "C:\Users\tlan2\PycharmProjects\david\jj.py", line 17, in <module>
    xiaoming.gamewdog()
TypeError: gamewdog() missing 1 required positional argument: 'dog'

zltzlt 发表于 2020-8-12 17:05:53

需要传入参数 wangcai

class Dog:
    def __init__(self,name):
      self.name = name
    def game(self):
      print("狗自己玩")
class DogKing(Dog):
    def game(self):
      print("上天玩")
class Person:
    def __init__(self,name):
      self.name = name
    def gamewdog(self,dog):
      print("%s和%s快乐的玩耍"(self.name,dog.name))
      dog.game()
wangcai = DogKing("wangcai")
xiaoming = Person("小明")
xiaoming.gamewdog(wangcai)

sunrise085 发表于 2020-8-12 17:09:23

两处错误:
1、第13行,少了一个%
2、第17行,gamewdog函数调用少了参数
class Dog:
    def __init__(self,name):
      self.name = name
    def game(self):
      print("狗自己玩")
class DogKing(Dog):
    def game(self):
      print("上天玩")
class Person:
    def __init__(self,name):
      self.name = name
    def gamewdog(self,dog):
      print("%s和%s快乐的玩耍"%(self.name,dog.name))#少了一个%
      dog.game()
wangcai = DogKing("wangcai")
xiaoming = Person("小明")
xiaoming.gamewdog(wangcai)#少了参数
页: [1]
查看完整版本: 新手问题求助