1

This is a program i have been working on but i have had some problems with. When this program is executed it prints the list of skaters and there information twice but i don't want that but i don't know how to fix it

# THESE ARE LISTS TO STORE INDIVIDUAL SKATERS AND THERE SCORES AND MEDIUM 

l_list = []
list = []

#STATMENT TO START WHILE LOOP AND TO END IT 

re = True 
while re == True:

    #STORING THE SKATERS NAME AND ADDING OPTION TO STOP ADDING SKATERS IN TO PROGRAM

    a_name = input("Please store the skaters name, if there are no more skaters enter 'stop'")

    #PRINTING ALL SKATERS AND THERE SCORES AND MEDIUM AND ADDING OPTION TO ADD MORE OR END PROGRAM

    if a_name == 'stop':
       #were pritn(l_list was)---------------------------------------------------------------------------------------
        for s in l_list:
            print(*s)
        exit = input("Would you like to exit? 'y/n'. ")
        if exit == 'y':
            break

    #WHILE LOOP TO GET SCORES AND STORE THEM IN A LIST

    scores = []
    repeat = True
    while repeat == True:

        #ASKING FOR THE SCORES AND A INPUT TO END THIS PROGRAMS FUNCTION WHICH WILL STORE THE SCORES TO THE LIST(scores)

        score = input("please type in your scores one at a time, and 'done' when finished.")
        if score == "done":

            repeat = False
        else:
            scores.append(score)


    #SORTING SCORES, REMOVING HIGHEST AND LOWEST SCORES 

    scores.sort()
    scores.remove(max(scores))
    scores.remove(min(scores))

    #STORING THE VARIABLE(a_name) AND THE LIST(scores) IN THE LIST(list)
    #THEN STORING THE LIST(list) INTO THE LIST(l_list)
    list.append(a_name)
    list.append(scores)
    l_list.append(list)

    #CONVERTING THE CONTENTS OF THE LIST(scores) IN TO FLOATS SO THAT IT CAN FIND TEH MEDIUM/AVERAGE SCORE USING LEN AND SUM
    #THEN STORING THE VARIABLE(med) INTO THE LIST(list)

    scores = [float(i) for i in scores]
    med = (sum(scores) / float(len(scores)))
    list.append(med)


#RUN FUNCTION  
start()

when i run the program with some test results this is what it prints:

[['thomas', ['2', '3', '4', '58'], 16.75, 'renee', ['2', '3', '4', '5', '6'], 4.0], ['thomas', ['2', '3', '4', '58'], 16.75, 'renee', ['2', '3', '4', '5', '6'], 4.0]]
Would you like to exit? 'y/n'.

as you can see it prints it twice which I don't want.

pushkin
  • 9,575
  • 15
  • 51
  • 95
T Renee
  • 11
  • 1
  • 1
    "list" is a [built-in](https://docs.python.org/3/library/functions.html). It is not recommended to use any of the built-ins as variable names as you are overwriting the built-in in your namespace. – Leo Jun 19 '18 at 03:04

2 Answers2

2

You need to recreate list each time through the loop.

On the first time through the loop, list and l_list are both empty: you get name and scores, append them to list and append that to l_list. All is good.

But then you go through the loop a second time for your next skater. You append their name and scores to list which already has the previous skater's name and scored. Then you append that whole thing to l_list again. Now l_list has two copies of list, and list has both skaters and their scores.

Adding list = [] before you call list.append() should resolve the issue.

Nikolas Stevenson-Molnar
  • 4,235
  • 1
  • 22
  • 31
2

You are off to a good start but I would like to warn you against using the variable name list. That is the name of a type in Python so instead, you can use lst or list_. In this case, I'd call it skater_list. This question covers all the names not to use for variables.

When you're adding names and scores to your lists, you never clear them out so you are adding them multiple times.

However, I don't think lists would actually be your most useful data structure here. I think a dict where the key is the name and the values are the scores would work best.

At the start, create a dict rather than your lists:

final_dict = {}

When printing the list out, print the dict instead:

    if a_name == 'stop':
       #were pritn(l_list was)---------------------------------------------------------------------------------------
        for name, scores in final_dict.items():
            score, med = scores
            print(name, score, med)

Remove your code to add the items to the lists

    scores.sort()
    scores.remove(max(scores))
    scores.remove(min(scores))

    scores = [float(i) for i in scores]
    med = (sum(scores) / float(len(scores)))

Instead, add them to a dictionary. Here I made the key the name of the skater and stored both all the remaining scores and the average score.

    final_dict[a_name] = (scores, med)

Note, you still need to clear out the score list:

    scores = []

The output here would be something like:

Adam [7.0, 8.0] 7.5
Joe [6.0, 7.0] 6.5
Zev
  • 3,423
  • 1
  • 20
  • 41
  • 1
    Agreed about not calling your list `list`. In addition to it being already used by Python, the name `list` doesn't say anything about what it is. `skater_list`, as @Zev suggests, is good. Or, something like `skater_data`. – Nikolas Stevenson-Molnar Jun 19 '18 at 03:14