编写包含以下内容的Player类的定义:
不需要定义构造函数。
这是我的代码:
class Player(object)
name = ""
score = 0
def set_name(self):
self.name = name
def set_score (self):
self.score = score
def get_name
return name
def get_score
return score
当我提交这个时,我的编程实验室给我的唯一反馈是“你几乎肯定应该使用:,(逗号)”和“编译器错误”。我不知道如何处理这些信息,所以任何帮助将不胜感激!提前谢谢
答案 0 :(得分:0)
class Player(object): #notice the colon
name = ""
score = 0
def set_name(self, name): #notice how it accepts (self, name)
self.name = name
def set_score (self, score): #notice how it accepts (self, score)
self.score = score
def get_name(self): #notice how it accepts (self) and has a colon
return self.name #returns self.name not just name
def get_score(self): #accepts (self) and has a colon
return self.score #returns self.score not just score
thisPlayer = Player()
thisPlayer.set_name("greg")
print(thisPlayer.get_name())
大多只是一堆语法错误
答案 1 :(得分:0)
以下更正;每个定义在行尾都需要冒号,你没有为set方法定义实例变量,在get方法定义中包含self,我总是尽量不为实例变量使用相同的变量名
class Player:
def __init__(self):
self.__name = ""
self.__score = 0
def set_name(self, name):
self.__name = name
def set_score(self, score):
self.__score = score
def get_name(self):
return self.__name
def get_score(self):
return self.__score