蓝大伟 发表于 2020-8-12 21:23:50

新手问题看一眼

class Game:
    topscore = 0
    def __init__(self,playername):
      self.playername = playername
    @staticmethod
    def help():
      print("帮助信息:让僵尸进门。")
    @classmethod
    def showtopscore(cls):
      print("历史记录%d"%cls.topscore)

    def start_game(self):
      print("%d开始游戏了!"%self.playername)
Game.help()
Game.showtopscore()
game = Game("小明")
game.start_game()




帮助信息:让僵尸进门。
历史记录0
Traceback (most recent call last):
File "C:\Users\tlan2\PycharmProjects\david\jj.py", line 17, in <module>
    game.start_game()
File "C:\Users\tlan2\PycharmProjects\david\jj.py", line 13, in start_game
    print("%d开始游戏了!"%self.playername)
TypeError: %d format: a number is required, not str

Process finished with exit code 1


咋回事,郁闷

zltzlt 发表于 2020-8-12 21:25:36

要格式化的是字符串,所以把 %d 改成 %s

class Game:
    topscore = 0
    def __init__(self,playername):
      self.playername = playername
    @staticmethod
    def help():
      print("帮助信息:让僵尸进门。")
    @classmethod
    def showtopscore(cls):
      print("历史记录%d"%cls.topscore)

    def start_game(self):
      print("%s开始游戏了!"%self.playername)
Game.help()
Game.showtopscore()
game = Game("小明")
game.start_game()
页: [1]
查看完整版本: 新手问题看一眼