python 3.6附加列表创建了重复项

时间:2018-01-17 21:50:16

标签: python python-3.6

我的程序将计算最终成绩,我相信所有这些都会更正,直到我创建多个条目。当我进入' y'为了进入另一个学生,第二个学生复制到第一个学生名单然后自我复制导致副本而不是我的第一个原始条目。所以不要让迈克,吉姆和玛丽玛丽,玛丽和玛丽。

#This program will calculate a classes final grades.

def get_grades():

    print("\nEnter grades for assignments 1-5 (0-20 PT scale)")

    assignment1 = eval(input("\nEnter assignment 1 grade: "))
    assignment2 = eval(input("Enter     ''     2 grade: "))
    assignment3 = eval(input("Enter     ''     3 grade: "))
    assignment4 = eval(input("Enter     ''     4 grade: "))
    assignment5 = eval(input("Enter     ''     5 grade: "))

    total = (assignment1 + assignment2 + assignment3 + assignment4 + assignment5)

    print("\nEnter the Midterm and Final exam grades (0-100 PT scale))")
    midterm = eval(input("\nMidterm grade: "))
    final = eval(input("Final grade: "))

    exam_avg = (final + midterm)/2

    print("\nEnter the participation grade (0-10 PT scale)")
    participation = int(input("Participation: "))

    final_grade = (total*0.45) + (exam_avg*0.45) + participation

    return final_grade

def determine_let_grade(final_grade):

    if final_grade > 100 or final_grade < 0:
        return "ERROR!"

    elif final_grade <= 100 and final_grade > 90:
        return "A"

    elif final_grade <= 89 and final_grade > 80:
        return "B"

    elif final_grade <= 79 and final_grade > 70:
        return "C"

    elif final_grade <= 69 and final_grade > 60:
        return "D"

    elif final_grade <= 59:
        return "F"   

def main():

    print("This program will calculate your students final grades.")

    studentList = []
    another_student = "y" 

    while another_student == 'y':
        name = input("\nEnter the student's name: ")
        net_id = int(input("Enter the student's Identification number: ")) 
        fnl_grade = get_grades()
        let_grade = determine_let_grade(fnl_grade)
        studentList.append([name, net_id, fnl_grade, let_grade])
        another_student = input("\nEnter y or n to add another student: ")

    for student in range(len(studentList)):
        print("\nStudent Name:", name.title() ,"\nStudent-ID:", net_id ,"\nFinal Grade:",fnl_grade ,"\nLetter Grade:", let_grade)
        print()
    input ("Please press Enter to quit the program")
main()

2 个答案:

答案 0 :(得分:1)

您在name循环中定义net_idfnl_gradelet_gradewhile。退出循环时,这些变量保留最后一次迭代的值。您只需在以下for循环中打印这些变量而不进行设置。您可能希望使用以下条件:

  for name, net_id, fnl_grade, let_grade in studentList:
      print("\nStudent Name:", name.title() ,"\nStudent-ID:", net_id ,"\nFinal Grade:",fnl_grade ,"\nLetter Grade:", let_grade)
      print()

答案 1 :(得分:0)

问题不在您的列表中,而是在您的循环中。

您需要使用正在循环播放的学生,而不是使用namenet_idfnl_gradelet_grade变量。此外,由于studentList是一个列表,您可以简单地循环它

for student in studentList:
    print("\nStudent Name:", student[0] ,"\nStudent-ID:", student[1] ,"\nFinal Grade:",student[2] ,"\nLetter Grade:", student[3])