python类。不确定我是否正确链接了函数。当我询问时,程序不打印
下面的代码是更大代码集的一部分。您将看到我在代码下面有一些测试命令。我正在测试,以确保课堂上所有的方法都有效。我的问题是,代码没有打印到指定的位置。任何人都知道我做错了什么。方法是否正确链接到类 多谢各位python类。不确定我是否正确链接了函数。当我询问时,程序不打印,python,class,Python,Class,下面的代码是更大代码集的一部分。您将看到我在代码下面有一些测试命令。我正在测试,以确保课堂上所有的方法都有效。我的问题是,代码没有打印到指定的位置。任何人都知道我做错了什么。方法是否正确链接到类 多谢各位 class Score: # class to hold a running score, from object to parameter # also to set number of scores that contribute to total of 1 def __i
class Score:
# class to hold a running score, from object to parameter
# also to set number of scores that contribute to total of 1
def __init__(self):
#initalizes the running score and score input accumilators
self.runScore = 0
self.scoreInputs = 0
def updateOne (self, amount):
#updates running score by amount and Score input by 1
self.runScore += amount
self.scoreInputs += 1
def updateMany(self,lst):
#updates running score by the sum of the list and score inputs by the amount of
# items in the list
self.runScore += sum(lst)
self.scoreInputs += len(lst)
def get(self):
#returns the current score based on total amount scored
return self.runScore
print(score.runScore)
def average(self):
aver = self.runScore // self.scoreInputs
#returns the average of the scores that have contributed to the total socre
return aver
print(score.aver)
agame = Score()
agame.updateOne(4)
agame.updateOne(2)
agame.updateMany([3,5,4,2,8])
agame.get()
agame.average()
我的输出
>>>
>>>
因为你什么也看不到
更新
因此,我删除了return语句并修复了一些其他错误,现在我有一个错误,这使我认为我没有将方法完全连接到类。但我还在想办法。有什么想法吗
class Score:
# class to hold a running score, from object to parameter
# also to set number of scores that contribute to total of 1
def __init__(self):
#initalizes the running score and score input accumilators
self.runScore = 0
self.scoreInputs = 0
def updateOne (self, amount):
#updates running score by amount and Score input by 1
self.runScore += amount
self.scoreInputs += 1
def updateMany(self,lst):
#updates running score by the sum of the list and score inputs by the amount of
# items in the list
self.runScore += sum(lst)
self.scoreInputs += len(lst)
def get(self):
#returns the current score based on total amount scored
#return self.runScore #debug test possible removal
print(Score.runScore)
def average(self):
aver = self.runScore // self.scoreInputs
#returns the average of the scores that have contributed to the total socre
#return aver #debug test possible removal
print(Score.aver)
agame = Score()
agame.updateOne(4)
agame.updateOne(2)
agame.updateMany([3,5,4,2,8])
agame.get()
agame.average()
输出
我有一个错误,如下所示:
Traceback (most recent call last):
File "C:\Users\christopher\Desktop\hw2.py", line 37, in <module>
agame.get()
File "C:\Users\christopher\Desktop\hw2.py", line 24, in get
print(Score.runScore)
AttributeError: type object 'Score' has no attribute 'runScore'
回溯(最近一次呼叫最后一次):
文件“C:\Users\christopher\Desktop\hw2.py”,第37行,在
阿加梅
get中第24行的文件“C:\Users\christopher\Desktop\hw2.py”
打印(Score.runScore)
AttributeError:类型对象“Score”没有属性“runScore”
打印语句必须在返回语句之前。返回后,程序将退出该功能。Areturn
语句类似于break
;返回所需变量后,退出上述函数
def foo():
return 'bar'
print 'foobar'
因此,您需要将
打印
s移到返回
s之前:
class Score:
# class to hold a running score, from object to parameter
# also to set number of scores that contribute to total of 1
def __init__(self):
#initalizes the running score and score input accumilators
self.runScore = 0
self.scoreInputs = 0
def updateOne (self, amount):
#updates running score by amount and Score input by 1
self.runScore += amount
self.scoreInputs += 1
def updateMany(self,lst):
#updates running score by the sum of the list and score inputs by the amount of
# items in the list
self.runScore += sum(lst)
self.scoreInputs += len(lst)
def get(self):
#returns the current score based on total amount scored
print(score.runScore)
return self.runScore
def average(self):
aver = self.runScore // self.scoreInputs
#returns the average of the scores that have contributed to the total socre
print(score.aver)
return aver
agame = Score()
agame.updateOne(4)
agame.updateOne(2)
agame.updateMany([3,5,4,2,8])
agame.get()
agame.average()
关键字
return
离开当前函数调用并返回一个值。
见
所以,你应该在函数返回之前完成所有操作,就像C一样。所以最后一个通牒是不使用返回语句,而是使用打印语句。 并将这些方法与自我(xxx)联系起来,而不是与分数联系起来
一切都解决了。谢谢大家在
get(self)
打印前返回。在average(self)
中也一样。谢谢@A.J。我已经修复了返回语句。我似乎遇到了方法与类完全连接的问题。我说得对吗?你知道我做错了什么吗?目前正在研究这个问题myself@ChristopherJakob我将开始关注这一点:)
class Score:
# class to hold a running score, from object to parameter
# also to set number of scores that contribute to total of 1
def __init__(self):
#initalizes the running score and score input accumilators
self.runScore = 0
self.scoreInputs = 0
def updateOne (self, amount):
#updates running score by amount and Score input by 1
self.runScore += amount
self.scoreInputs += 1
def updateMany(self,lst):
#updates running score by the sum of the list and score inputs by the amount of
# items in the list
self.runScore += sum(lst)
self.scoreInputs += len(lst)
def get(self):
#returns the current score based on total amount scored
print(score.runScore)
return self.runScore
def average(self):
aver = self.runScore // self.scoreInputs
#returns the average of the scores that have contributed to the total socre
print(score.aver)
return aver
agame = Score()
agame.updateOne(4)
agame.updateOne(2)
agame.updateMany([3,5,4,2,8])
agame.get()
agame.average()