类型错误:“函数”对象不可下标 Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28121499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: 'function' object is not subscriptable Python
提问by Stephen de Riel
I got an assignment in my Python 1 class consisting of this:
我在 Python 1 课程中得到了一个作业,其中包括:
////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////// //////////////////////////////////////////////
You are a score keeper for 20 ice hockey players. Implement the code to keep track of their scores using a list. Program accepts integers as input and it is called YI_ScoreKeeper.py. Simulate a game by entering a good number of scores.
您是 20 名冰球运动员的记分员。实现代码以使用列表跟踪他们的分数。程序接受整数作为输入,它被称为 YI_ScoreKeeper.py。通过输入大量分数来模拟游戏。
Here are two pics. she gave(I don't have enough rep. to post them as images)
这是两张照片。她给了(我没有足够的代表将它们作为图片发布)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////// ///////////////////////////////////////////////// /////////
So far, my code is this:
到目前为止,我的代码是这样的:
def scorekeeper():
Scorekeeper = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
return Scorekeeper
def addscore(Scorekeeper):
Addscore = int(input("what player scored a goal?"))
Addscore = Addscore - 1
(Scorekeeper[Addscore]) = ((Scorekeeper[Addscore]) + 1)
return Scorekeeper
def histogram(Scorekeeper):
print("\nCreating a histogram from values: ")
print("%s %10s %10s" %("Element", "Ranking", "Histogram"))
for i in range(len(Scorekeeper)):
print("%7d%5d %-s" % (i +1, Scorekeeper[i], "*" * Scorekeeper[i]))
def main():
Scorekeeper = scorekeeper()
endgame = 'n'
while endgame == 'n':
Addscore = addscore(scorekeeper)
endgame = input("Has the game ended? y/n")
histogram(scorekeeper)
main()
///////////////////////////////////////////////////////////////////////////////////////// I keep on getting this error:
///////////////////////////////////////////////// ///////////////////////////////////// 我不断收到此错误:
Traceback (most recent call last):
File "C:/Python34/scorekeeper.py", line 27, in <module>
main()
File "C:/Python34/scorekeeper.py", line 22, in main
Addscore = addscore(scorekeeper)
File "C:/Python34/scorekeeper.py", line 11, in addscore
(Scorekeeper[Addscore]) = ((Scorekeeper[Addscore]) + 1)
TypeError: 'function' object is not subscriptable
////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////// //////////////////////
Help? I'm not sure what I'm doing wrong.
帮助?我不确定我做错了什么。
回答by Eithos
You made a small mistake here:
你在这里犯了一个小错误:
def main():
Scorekeeper = scorekeeper()
endgame = 'n'
while endgame == 'n':
addscore(Scorekeeper) # This was set to scorekeeper
endgame = input("Has the game ended? y/n")
histogram(Scorekeeper) # Here too
Rather than calling the addscore
function with the Scorekeeper
list you'd already returned from scorekeeper()
, you were just sending in the function scorekeeper
.
而不是addscore
使用Scorekeeper
您已经从 返回的列表调用该函数scorekeeper()
,您只是发送了该函数scorekeeper
。
This is why you were getting the error message; you were expecting to modify the ScoreKeeper
list with the index syntax you used, but instead you had a function object. Hope this clears it up.
这就是您收到错误消息的原因;您希望ScoreKeeper
使用您使用的索引语法修改列表,但您有一个函数对象。希望这能解决问题。
Edit:
编辑:
As pointed out by @KevinJ.Chase:
正如@KevinJ.Chase所指出的:
The Addscore
in Addscore = addscore(scorekeeper)
didn't change anything. In Python, references to objectsare passed in as function parameters. What this means is that the passed parameter (inside the function) is a new identifier binded to the same object. While the logic involved here is not that terribly important to understand when passing immutable objects like an Int
, a Str
(etc.), passing a list
means the new identifier inside the function references the same mutable data (the list
). In this case, that mutable data was modified inside the function call; so returning it was indeed unnecessary.
将Addscore
在Addscore = addscore(scorekeeper)
没有改变任何东西。在 Python 中,对对象的引用作为函数参数传入。这意味着传递的参数(在函数内部)是绑定到同一对象的新标识符。虽然在传递像 an Int
、 a Str
(等)这样的不可变对象时理解这里涉及的逻辑并不是那么重要,但传递 alist
意味着函数内的新标识符引用相同的可变数据(list
)。在这种情况下,可变数据在函数调用中被修改;所以返回它确实是不必要的。
回答by Padraic Cunningham
You are not calling the function to access the list:
您不是调用函数来访问列表:
(Scorekeeper()[Addscore]) = ((Scorekeeper()[Addscore]) + 1)
You also need to call the function in your loop:
您还需要在循环中调用该函数:
for i in range(len(Scorekeeper())):
print("%7d%5d %-s" % (i +1, Scorekeeper()[i], "*" * Scorekeeper()[i]))
But really you should just declare a list outside of the functions and just access the list directly and forget about using the functions:
但实际上,您应该只在函数之外声明一个列表,然后直接访问该列表而忘记使用这些函数:
scorekeeper = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
scorekeeper = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
You can change the histogram function yourself but this is an idea of how to write simpler more straight forward code:
您可以自己更改直方图函数,但这是如何编写更简单更直接的代码的想法:
def add_score():
score = int(input("what player scored a goal?"))
return score
def main():
endgame = 'n'
score_keeper = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
while endgame == 'n':
scorer = add_score()
score_keeper[scorer] += 1
endgame = input("Has the game ended? y/n")
When taking input and especially casting you should use a try/exceptto validate so I would add a second while loop in add_score
with a try except.
当接受输入,尤其是强制转换时,你应该使用try/except来验证,所以我会添加第二个 while 循环add_score
和 try except。