TypeError:'功能'对象不是可下载的Python

时间:2015-01-24 01:58:32

标签: python python-3.x typeerror

我在我的Python 1类中得到了一个由此组成的作业:

/////////////////////////////////////////////// ///////////////////////////////////////////////// < / p>

你是20名冰球运动员的得分手。实施代码以使用列表跟踪他们的分数。程序接受整数作为输入,它被称为YI_ScoreKeeper.py。通过输入大量分数来模拟游戏。

这是两张照片。她给了(我没有足够的代表将它们发布为图像)

https://e.edim.co/29892634/screen_shot_2015_01_09_at_9_30_52_am_t.png?Expires=1422066625&Signature=Ql0P778epTnFNUD~4AiZtwr5Gip~JTgohs8ShfVD5Yzvghot0hTATBAbktvD6whm~WG8a9gSJ98fihD81NLyPf7E615hMKaYOBIxLJZp4M1l1EBAYHGZZeDY6xblYlb-PelzwDo8USbcCuq8OAIioaiMrWeQ2WV5X4YUmqwZHbgqPGYvXP~nhupH7qoTdLUagdleySQ8S8BhG6at0YeHwd5pgwMh-Lq3hJ97lfmsrhYeWhG~yr6t3WpzZmgWPVg1WRo1lbPNC5Y91952iDub-20aZuK2sDngcTSf7BnBfn6laIeN~Ib3uhX~KJe9tcWs0EY~CwiDl~-rXIB73f5uIQ__&Key-Pair-Id=APKAIJNVNRBLLSTGN23Q

https://e.edim.co/29892634/screen_shot_2015_01_09_at_9_31_36_am_t.png?Expires=1422066625&Signature=Bgf-JQNNMnkHT3Taocc-rxqo4F2BJLAGdcl-qZpJHcFWBov0rvTvktQxBklJGIXk~Y7or1KFJQIvLWw2Fsr002XtB0N7qqQZLl3FRc8nmEvE~sIt0atsZmj4V8Fq9OSkO6UjMgHroIBtl2NlhRJ2DoSWoDyoMT13ODN7AUuyClwwFB5PlJW2TtUeF5mOUyN0eOXzYV-jk4oEyptnK2RYwbSo-b-mY677mkK66iBiPCkcSQPciJs3VOxfopNhshWYb01CbYcDI0inJ2FqD3t8WLQfzmzY8HRy8A2aFgrJIM1OsAh9xKcb49zRSsfUP9W0lxmh007wxC8dLbu06Y1XVw__&Key-Pair-Id=APKAIJNVNRBLLSTGN23Q

/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////

到目前为止,我的代码是:

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()

/////////////////////////////////////////////// ////////////////////////////////////////// 我一直在收到这个错误:

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

/////////////////////////////////////////////// /////////////////////////

帮助?我不确定我做错了什么。

2 个答案:

答案 0 :(得分:3)

你在这里犯了一个小错误:

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

您没有使用addscore已经从Scorekeeper返回的scorekeeper()列表调用scorekeeper函数,而只是发送函数ScoreKeeper

这就是您收到错误消息的原因;您希望使用您使用的索引语法修改Addscore列表,但您有一个函数对象。希望这可以解决它。

编辑:

正如 @ KevinJ.Chase 所指出的那样:

Addscore = addscore(scorekeeper)中的Int没有改变任何内容。在Python中,对象的引用作为函数参数传入。这意味着传递的参数(在函数内)是绑定到同一对象的新标识符。虽然这里涉及的逻辑在传递Strlist(等)等不可变对象时理解并不是非常重要,但传递list意味着函数引用中的新标识符相同的可变数据({{1}})。在这种情况下,在函数调用中修改了可变数据;所以回来确实没必要。

答案 1 :(得分:2)

您没有调用该函数来访问列表:

(Scorekeeper()[Addscore]) = ((Scorekeeper()[Addscore]) + 1)

您还需要在循环中调用该函数:

 for i in range(len(Scorekeeper())):
        print("%7d%5d %-s" % (i +1, Scorekeeper()[i], "*" * Scorekeeper()[i]))

但实际上你应该在函数之外声明一个列表,直接访问列表并忘记使用函数:

scorekeeper = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

您可以自己更改直方图功能,但这是如何编写更简单更直接的代码的想法:

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")

在接受输入并特别是强制转换时,您应使用try/except进行验证,以便在add_score中添加第二个while循环,但尝试除外。