如何将列表中的数字总和保存到新列表中?

时间:2019-05-03 11:49:03

标签: python python-3.x

我正在编写一个代码,该代码创建一个存储多个课程名称的列表。还有另一个列表,用于存储学生姓名及其分数。我正在尝试生成一份报告,以显示列表中的所有课程和学生总分。

我尝试创建第三个列表,该列表将存储带有学生姓名和分数的另一门课程,然后仅显示该课程和提供该课程的学生的总分数。

def menu():

    choice = input("""
    1: Enter course
    2: Enter score figure
    3: Generate report

    Please enter your choice: """)
    print("------------------------------------------------------------")

    if choice == "1" :
            entercoursedetails()
    elif choice == "2":
            enterstudentdetails()
    elif choice == "3":
            generatereport()
    else:
            print("You must only select either 1,2 or 3.")
            print("Please try again")
            menu()

def entercoursedetails():
    #user is prompted to input all the required fields
    print("Enter course name")
    coursename=input()

    if (coursename not in Currentcourse) :
             Currentcourse.append(coursename)
             informationList.append(coursename)# {record all course names}

    index = Currentcourse.index(coursename)        

   return coursename

menu()        

def enterstudentdetails():

    name_array = list()
    score_array = list()

    print ("score Reporting")
    print (" Current course:", Currentcourse)
    def check_continue():


        response = input('Continue? [y/n] ')

        if response == 'n':
            return False
        elif response == 'y':
            return True
        else:
            print('Please select a correct answer [y/n]')
            return check_continue()


    while(True):

        std_name = input('Name: ')
        score_record = int(input('score: '))

        name_array.append(std_name)
        score_array.append(score_record)

        if not check_continue():
            break
        else:
            continue

    for name, score in zip(name_array, score_array):
        print(name, '\t', score)


def generatereport():
    persum = [0 for i in range(100)] 
    for item in informationList :


        persum[Currentcourse.index(item[1])] += float(item[2]) 

    for i in range (len(Currentcourse)) :     
        print(Currentcourse[i], "    ", persum[i])
        print("-----------------------------")

    for item in informationList :
        print(item[0], "   ", item[2])

    print("-----------------------------")
    print("Total Score:     ", sum(persum))
    print("-----------------------------")

选择“生成报告”时,其含义是显示

_____________________
Maths        | 98   |

Science      | 30.5 |
_____________________
Peter        | 52   |

Paul         | 46   |

Abba         | 30.5 |
_____________________
Total sales  | 128.5|
_____________________
彼得和保罗只提供数学。 abba只提供科学。

0 个答案:

没有答案