3

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.

2
  • I think Pandas would be better Commented Nov 5, 2019 at 2:49
  • 1
    @ggorlen I just updated the code Commented Nov 5, 2019 at 3:09

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

Using the code suggested, my updated formatting looks like this:

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}{:>1}".format(student[1][0], student[1][1:]))

and this is my new output:

Grades for Class With a Cool Name
Grade Percentage     Letter Grade
     98.99                 A 
     65.17                 D 
     68.71                 D+
     78.15                 C+
     85.97                 B 
     91.48                 A-
     57.61                 F 
      73.5                 C 
     70.06                 C-
     75.54                 C 
     50.14                 F 
     59.78                 F 
     80.49                 B-
      67.5                 D+
     90.99                 A-
     59.28                 F 
     50.37                 F 
     52.03                 F 
     60.42                 D-
     60.38                 D-
     81.54                 B-
     92.49                 A-
     55.96                 F 
     73.52                 C 
     85.03                 B 
     80.36                 B-
      80.4                 B-
      71.8                 C-
     81.64                 B-
     58.89                 F 
     67.06                 D+
     79.22                 C+
     50.72                 F 
     64.91                 D 
     84.82                 B 

Thanks for the help! My code is working perfectly now and I don't have to obsess over that little formatting error.

1 Comment

Glad it works! And, I was already thinking of fixing the spacing because it bothered me too!
0

If you don't want to use any other libraries, there is a pretty simple solution.

Update your print statement like this:

print('{:10}'.format(student[0]), "{:>17}".format(student[1][0]), "{:<1}".format(student[1][1:]))

and you'll get output like:

Grades for Class With a Cool Name
Grade Percentage     Letter Grade
jeff                       A +
sally                      A
tim                        B -

This works by splitting your student[1] value (the grade) into two pieces. First, student[1][0] gets the letter and we right justify it with the same spacing you had. Next, we will pull off the plus/minus if it exists with student[1][1:]. This is a slice which goes from index 1 to the end. If index 1 is the non-inclusive end, then this slice is "empty" so nothing happens.

EDIT: To remove spaces, you can put the {} items in one statement and separate the arguments with commas:

print('{:10}'.format(student[0]), "{:>17}{:<1}".format(student[1][0], student[1][1:]))

4 Comments

This is awesome and actually really helped the alignment, but is there anyway to maintain having no space between the letter value and the +/-?
I answered this in the edit. Glad it could help and don't forget to accept a solution!
Wouldn’t it be better to use f-strings?
While f-strings are an option, I'm not sure how to argue which is "better" beyond preference for a beginner. Since their question used the string format method, an answer keeping in what they are familiar with seemed better than introducing a new technique. If you want, you can post an answer using f-strings in an example so that others can see more options and choose which one fits their preference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.