溜冰者评分系统

时间:2018-06-19 02:37:52

标签: python list

这是我一直在努力的程序,但我遇到了一些问题。执行此程序时,它会打印滑冰者列表并有两次信息,但我不想要,但我不知道如何修复它

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

当我使用一些测试结果运行程序时,这就是它打印的内容:

[['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'.

你可以看到它打印两次,我不想要。

2 个答案:

答案 0 :(得分:2)

每次循环都需要重新创建list

第一次循环时,listl_list都是空的:您获得姓名和分数,将其附加到list并将其附加到l_list。一切都很好。

然后你再次为你的下一个选手穿过循环。您将他们的姓名和分数附加到list已经拥有上一个滑冰运动员姓名并得分的l_list。然后再将整个事物追加到l_list。现在list有两份listlist = []有两名选手和他们的分数。

在致电list.append()之前添加|Company |Period |Value | ------------------------------- |CompA |January |10000 | |CompB |January |99999 | |CompA |February |88888 | 可以解决问题。

答案 1 :(得分:2)

你有一个良好的开端,但我想警告你不要使用变量名list。这是Python中类型的名称,因此您可以使用lstlist_。在这种情况下,我称之为skater_listThis question涵盖了不用于变量的所有名称。

当您在列表中添加姓名和分数时,您永远不会将其清除,因此您需要多次添加它们。

但是,我不认为列表实际上是您最有用的数据结构。我认为一个dict,其中键是名称,值是得分最好。

首先,创建一个dict而不是您的列表:

final_dict = {}

打印列表时,请改为打印dict

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

删除您的代码以将项目添加到列表

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

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

相反,将它们添加到字典中。在这里,我将钥匙设为滑冰运动员的名字,并存储了所有剩余的分数和平均分数。

    final_dict[a_name] = (scores, med)

注意,您仍然需要清除分数列表:

    scores = []

这里的输出类似于:

Adam [7.0, 8.0] 7.5
Joe [6.0, 7.0] 6.5