简体   繁体   中英

Tables in python- not sure why it isn't working

students = []
scores = []
name = input("Please enter the student's name: ")
while name != "":
    scores.append([0,0,0,0])
    students.append(name)
    test1 = int(input("What was the score of the first test?: "))
    test2 = int(input("What was the score of the second test?: "))
    total = test1 + test2
    percentage = (total / 80) * 100
    scores.append([test1,test2,total,percentage])
    name = input("Please enter the student's name or press enter: ")
print("+----------+----------+----------+----------+----------+")
print("|Name       |Score 1   |Score 2   |Total     |Percent  |")
print("+----------+----------+----------+----------+----------+")
length = len(students)
for count in range(0,length):
    print("|%-10s|%10i|%10i|%10i|%10f|" %(students[count],scores[count][0],scores[count][1],scores[count][2],scores[count][3]))
    print("+----------+----------+----------+----------+----------+")

The code is supposed to allow a user to enter a student and their test scores, then calculating the total score and percentage. When they press enter when it asks for the student's name, a table should be printed with the names and scores etc.

The only problem is that when it comes to printing out the table, it prints out the name of the first student but the scores, total and percentage for the first student will be 0. Then for the second student the scores, total and percentage will be what was actually the first student's.

This is the outcome of my code:

Please enter the student's name: tk
What was the score of the first test?: 33
What was the score of the second test?: 32
Please enter the student's name or press enter: kk
What was the score of the first test?: 34
What was the score of the second test?: 35
Please enter the student's name or press enter: 
+----------+----------+----------+----------+----------+
|Name       |Score 1   |Score 2   |Total     |Percent  |
+----------+----------+----------+----------+----------+
|tk        |         0|         0|         0|  0.000000|
+----------+----------+----------+----------+----------+
|kk        |        33|        32|        65| 81.250000|
+----------+----------+----------+----------+----------+

As Yu Hao stated in his comment, the issue is the line

scores.append([0,0,0,0])

You add a row of zeros to your table at the beginning of your first loop. If you were to test more than two students, you would find that every odd student was listed as all zeros: for every "student" entry you make, you make two "scores" entry - one that is "0,0,0,0" and one with the actual scores.

If you delete that statement, your code should run as you expect.

Incidentally - the second loop is a great example of what the "zip" built-in is intended for. https://docs.python.org/3/library/functions.html#zip

Instead of:

for count in range(0,length):
    print("|%-10s|%10i|%10i|%10i|%10f|" %(students[count],scores[count][0],scores[count][1],scores[count][2],scores[count][3]))

try:

for student,score in zip(students,scores):
    print("|%-10s|%10i|%10i|%10i|%10f|" %(student,score[0],score[1],score[2],score[3]))

It's probably easier to read with the zip, which makes it easier for you to edit later. (therefore more "Pythonic")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM