Reputation: 53
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.
Upvotes: 3
Views: 65
Reputation: 53
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.
Upvotes: 1
Reputation: 609
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:]))
Upvotes: 0
Reputation: 71610
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)
Upvotes: 1