Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting lists in Python

I am very new to coding, and I am trying to create a project that lists grade percentages along with their letter equivalents. I am having a lot of trouble formatting it to look nice, though. This is most of the code I currently have:

(Edited to show more data)

def main():
    grade_list = create_grades()
    print_list(grade_list)

def create_grades():
    grade_list = []
    for student in range(35):
        percentage_grade = round(random.uniform(50, 100), 2)
        letter_grade = get_grade(percentage_grade)
        dumb_tuple = (percentage_grade, letter_grade)
        grade_list.append(dumb_tuple)

list_title = "Grades for Class With a Cool Name"
def print_list(grade_list):
    print(list_title)
    print("{:20}".format("Grade Percentage"), "Letter Grade")
    for student in grade_list:
        print('{:10}'.format(student[0]), "{:>17}".format(student[1]))

and this is the resulting output:

Grades for Class With a Cool Name
Grade Percentage     Letter Grade
     93.44                 A
     99.58                 A
     67.37                D+
     79.07                C+
     64.75                 D
     79.42                C+
      97.2                 A
     73.37                 C
     86.33                 B
     83.56                 B
     98.89                 A
     68.74                D+
     76.03                 C
     63.75                 D
     77.43                C+
     51.32                 F
     53.18                 F
     61.56                D-
      65.4                 D
     94.48                 A
     85.96                 B
     92.62                A-
     90.51                A-
      91.7                A-
     50.76                 F
     67.93                D+
     52.98                 F
     81.85                B-
     60.91                D-
     84.71                 B
     63.74                 D
     61.64                D-
     74.21                 C
     97.99                 A
     72.95                C-

All I am trying to accomplish now is to make the letters line up, rather than having them become a jagged line when there is a plus or minus added to the value. I know this may seem like a relatively simple question, but none of my google research has helped me much, as I don't understand a lot of code yet. Any responses help.

like image 509
Shayla Avatar asked Aug 06 '26 11:08

Shayla


1 Answers

If pandas is ok with you:

import pandas as pd
df = pd.DataFrame(grade_list, columns=['Grade Percentage', 'Letter Grade'])
print(list_title)
print(df)
like image 156
U12-Forward Avatar answered Aug 08 '26 02:08

U12-Forward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!