1

I got an assignment in my Python 1 class consisting of this:

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

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.

Here are two pics. she gave(I don't have enough rep. to post them as images)

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

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

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.

0

2 Answers 2

3

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.

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.

Edit:

As pointed out by @KevinJ.Chase:

The Addscore in Addscore = addscore(scorekeeper) didn't change anything. In Python, references to objects are 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, the capital-A Addscore variable is never used, so the small-a addscore function's return value is thrown away every time. The code only works because the return value of addscore is completely useless: addscore mutates the list it's given, so returning that mutated list gives the caller a list it already has (it's how the caller passed the list to addscore in the first place). Storing that return value then creates the illusion that Scorekeeper and Addscore are two different lists, when they are actually just one list. addscore should return nothing.
@KevinJ.Chase Guilty as charged. I noticed it, but I tried keeping the answer as relevant to the problem at hand. I'll clean it up, thanks.
2

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]

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/except to validate so I would add a second while loop in add_score with a try except.

7 Comments

@Eithos, wrong again I am afraid. look at what is passed in to the functions
That's just the parameter. In main(), you can clearly see the function scorekeeper returns a list, which is what he meant to pass off to all the other functions. Edit: Technically, you're right. But, I mean... it's obvious that this was a typo on his part isn't it?
@Eithos, it is late, I am not going to argue, you are incorrect . If you actually ran the code with my changes you would see that it works. You answer does not even address the many errors
Don't take offense, please. I'm sorry if I come across as purposely annoying--it's not my intention. I just ran the fixes you made to his code: The problem is each time you call that function again, you're geting a new list, so you aren't keeping score. Your second example works fine (with the proposed alterations), but I think you meant for this scorekeeper[scorer] to be this score_keeper[scorer]
Yes thanks, a typo, not offended just too tired to argue, yes the code makes no sense but I was just showing why it happened and how to fix it, it was easier to give an example of how to simplify the code than try to point out and fix all the logic issues with the OP's code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.