How To Create A Table in Python

Baroness Sledjoys picture Baroness Sledjoys · Nov 1, 2015 · Viewed 45.8k times · Source

I've posted this before, but I'm reposting it with more detailed information.

This is my assignment: enter image description here

enter image description here

And, so far, this is my code:

# Define a function that caculates average test scores
def calculate_average(score1, score2, score3, score4, score5):
    total = score1 + score2 + score3 + score4 + score5
    average = total % 5
    print(average)

# Define a function that determines a letter grade
# Based on the given score
def determine_grade(score):
    if score < 60:
        print 'F'
    elif score => 60 or score <= 69:
        print 'D'
    elif score => 70 or score <= 79:
        print 'C'
    elif score => 80 or score <= 89:
        print 'B'
    elif score => 90 or score <= 100:
        print 'A'
# Define a function that prompts the user to input names and test scores
def input_data():
    score1 = input('Enter score 1:')
    name1 = input('Enter name 1:')
    score2 = input('Enter score 2:')
    name2 = input('Enter name 2:')
    score3 = input('Enter score 3:')
    name3 = input('Enter name 3:')
    score4 = input('Enter score 4:')
    name4 = input('Enter name 4:')
    score5 = input('Enter score 5:')
    name5 = input('Enter name 5:')


# Define a function that displays a letter grade for each score
# followed by the student's name and the average test score
def display_menu():

As I said in the first post, I don't have a real problem with anything except setting up that table. I'm really confused!

I asked my instructor (this is an online course, BTW), and he said: "Its just using the print operator, like you have done in previous modules, your printing text , variables and the return value of a function."

Now I'm just wondering where to start.

EDIT: I've updated my code this:

# Define a function that prompts the user to input names and test scores
def input_data():
    score1 = input('Enter score 1:')
    name1 = input('Enter name 1:')
    score2 = input('Enter score 2:')
    name2 = input('Enter name 2:')
    score3 = input('Enter score 3:')
    name3 = input('Enter name 3:')
    score4 = input('Enter score 4:')
    name4 = input('Enter name 4:')
    score5 = input('Enter score 5:')
    name5 = input('Enter name 5:')

# Define a function that determines a letter grade
# Based on the given score
def determine_grade(score):
    if score < 60:
        print ('F')
    elif 60 <= score <= 69:
        print ('D')
    elif  70 <= score <= 79:
        print ('C')
    elif 80 <= score <= 89:
        print ('B')
    elif 90 <= score <= 100:
        print ('A')

# Define a function that caculates average test scores
def calculate_average(score1, score2, score3, score4, score5):
    total = score1 + score2 + score3 + score4 + score5
    average = total / 5
    print(average)

# Define a function that displays a letter grade for each score
# followed by the student's name and the average test score
def display_menu():
    for x in range(10):
        print("{:<10}".format("{:0.1f}".format(x)), end='')
    print ("Name\t\t\tnumeric grade\t\tletter grade")
print ("---------------------------------------------------------------")
print ("%s:\t\t\t%f\t\t%s") % ('name1', 'score1', determine_grade)
print ("%s:\t\t\t%f\t\t%s") % ('name2', 'score2', determine_grade)
print ("%s:\t\t\t%f\t\t%s") % ('name3', 'score3', determine_grade)
print ("%s:\t\t\t%f\t\t%s") % ('name4', 'score4', determine_grade)
print ("%s:\t\t\t%f\t\t%s") % ('name5', 'score5', determine_grade)
print ("---------------------------------------------------------------")

But when I run it: enter image description here

EDIT #2: This is my code currently:

# Define a function that prompts the user to input names and test scores
def input_data():
    score1 = input('Enter score 1:')
    name1 = input('Enter name 1:')
    score2 = input('Enter score 2:')
    name2 = input('Enter name 2:')
    score3 = input('Enter score 3:')
    name3 = input('Enter name 3:')
    score4 = input('Enter score 4:')
    name4 = input('Enter name 4:')
    score5 = input('Enter score 5:')
    name5 = input('Enter name 5:')

# Define a function that caculates average test scores
def calculate_average(score1, score2, score3, score4, score5):
    total = score1 + score2 + score3 + score4 + score5
    average = total / 5
    print(average)

# Define a function that determines a letter grade
# Based on the given score
def determine_grade(score):
    if score < 60:
        print ('F')
    elif 60 <= score <= 69:
        print ('D')
    elif  70 <= score <= 79:
        print ('C')
    elif 80 <= score <= 89:
        print ('B')
    elif 90 <= score <= 100:
        print ('A')



# Define a function that displays a letter grade for each score
# followed by the student's name and the average test score
def display_menu():
   print ("Name\t\t\tnumeric grade\t\tlettergrade")
print ("---------------------------------------------------------------")
print ("%s:\t\t\t%f\t\t%s" % ('name1', 93, 'A'))
print ("%s:\t\t\t%f\t\t%s" % ('name2', 89, 'B'))
print ("%s:\t\t\t%f\t\t%s" % ('name3', 76, 'C'))
print ("%s:\t\t\t%f\t\t%s" % ('name4', 58, 'F'))
print ("%s:\t\t\t%f\t\t%s" % ('name5', 98, 'A'))
print ("---------------------------------------------------------------")
print (calculate_average)

And this is what happens when I run it:

enter image description here

Now, I have mainly two problems:

1) How do I get the input statements to execute and enter the data BEFORE the table is displayed?

2) How do I convert the numbers displayed so that they are in the '.2f' format? (I've already tried a few ways and none of them worked).

HOPEFULLY THE FINAL EDIT: I'm getting really close to the solution, but need help with a few more things.

Here is my code:

# Define a function that prompts the user to input names and test scores

score = input('Enter score 1:')
name1 = input('Enter name 1:')
score = input('Enter score 2:')
name2 = input('Enter name 2:')
score = input('Enter score 3:')
name3 = input('Enter name 3:')
score = input('Enter score 4:')
name4 = input('Enter name 4:')
score = input('Enter score 5:')
name5 = input('Enter name 5:')




# Define a function that determines a letter grade
# Based on the given score
def determine_grade(score):
    if score < 60:
        print ('F')
    elif 60 <= score <= 69:
        print ('D')
    elif  70 <= score <= 79:
        print ('C')
    elif 80 <= score <= 89:
        print ('B')
    elif 90 <= score <= 100:
        print ('A')

determine_grade(score)


# Define a function that caculates average test scores
def calculate_average(score):
    total = score + score + score + score + score
    average = total / 5
    print(average)

calculate_average(score)

# Define a function that displays a letter grade for each score
# followed by the student's name and the average test score
def display_menu():
   print ("Name\t\t\tnumeric grade\t\tlettergrade")
print ("---------------------------------------------------------------")
print ("%s:\t\t\t%f\t\t%s" % ('name1', 'score', determine_grade('score')))
print ("%s:\t\t\t%f\t\t%s" % ('name2', 'score', determine_grade('score')))
print ("%s:\t\t\t%f\t\t%s" % ('name3', 'score', determine_grade('score')))
print ("%s:\t\t\t%f\t\t%s" % ('name4', 'score', determine_grade('score')))
print ("%s:\t\t\t%f\t\t%s" % ('name5', 'score', determine_grade('score')))
print ("---------------------------------------------------------------")
calculate_average(score)

And this is what happens when I press F5:

enter image description here

GUYS, I'M ALMOST DONE JUST NEED HELP WITH FORMATTING: I created another file so I could reorganize my code a bit, so that's why there's no comments. This is what I have:

score1 = float(input('Enter score 1:'))
name1 = input('Enter name 1:')
score2 = float(input('Enter score 2:'))
name2 = input('Enter name 2:')
score3 = float(input('Enter score 3:'))
name3 = input('Enter name 3:')
score4 = float(input('Enter score 4:'))
name4 = input('Enter name 4:')
score5 = float(input('Enter score 5:'))
name5 = input('Enter name 5:')


def determine_letter_grade1(score1):
    if score1 < 60.0:
        print ('F')
    elif 60.0 <= score1 <= 69.0:
        print ('D')
    elif  70.0 <= score1 <= 79.0:
        print ('C')
    elif 80.0 <= score1 <= 89.0:
        print ('B')
    elif 90.0 <= score1 <= 100.0:
        print ('A')

def determine_letter_grade2(score2):
    if score2 < 60.0:
        print ('F')
    elif 60.0 <= score2 <= 69.0:
        print ('D')
    elif  70.0 <= score2 <= 79.0:
        print ('C')
    elif 80.0 <= score2 <= 89.0:
        print ('B')
    elif 90.0 <= score2 <= 100.0:
        print ('A')

def determine_letter_grade3(score3):
    if score3 < 60.0:
        print ('F')
    elif 60.0 <= score3 <= 69.0:
        print ('D')
    elif  70.0 <= score3 <= 79.0:
        print ('C')
    elif 80.0 <= score3 <= 89.0:
        print ('B')
    elif 90.0 <= score3 <= 100.0:
        print ('A')

def determine_letter_grade4(score4):
    if score4 < 60.0:
        print ('F')
    elif 60.0 <= score4 <= 69.0:
        print ('D')
    elif  70.0 <= score4 <= 79.0:
        print ('C')
    elif 80.0 <= score4 <= 89.0:
        print ('B')
    elif 90.0 <= score4 <= 100.0:
        print ('A')

def determine_letter_grade5(score5):
    if score5 < 60.0:
        print ('F')
    elif 60.0 <= score5 <= 69.0:
        print ('D')
    elif  70.0 <= score5 <= 79.0:
        print ('C')
    elif 80.0 <= score5 <= 89.0:
        print ('B')
    elif 90.0 <= score5 <= 100.0:
        print ('A')

average = (score1 + score2 + score3 + score4 + score5) / 5.0

def determine_letter_grade_avg(average):
    if average < 60.0:
        print ('F')
    elif 60.0 <= average <= 69.0:
        print ('D')
    elif  70.0 <= average <= 79.0:
        print ('C')
    elif 80.0 <= average <= 89.0:
        print ('B')
    elif 90.0 <= average <= 100.0:
        print ('A')

def display_menu():
    for x in range(10):
        print("{:<10}".format("{:0.1f}".format(x)), end='')
    print ("Name\t\t\tnumeric grade\t\tletter grade")
print ("---------------------------------------------------------------")
print ("%s:\t\t\t%f\t\t%s" % (name1, score1, determine_letter_grade1(score1)))
print ("%s:\t\t\t%f\t\t%s" % (name2, score2, determine_letter_grade2(score2)))
print ("%s:\t\t\t%f\t\t%s" % (name3, score3, determine_letter_grade3(score3)))
print ("%s:\t\t\t%f\t\t%s" % (name4, score4, determine_letter_grade4(score4)))
print ("%s:\t\t\t%f\t\t%s" % (name5, score5, determine_letter_grade5(score5)))
print ("---------------------------------------------------------------")
print ('Average Score:', average, determine_letter_grade_avg(average))

And when I run it:

enter image description here

Answer

Steven Summers picture Steven Summers · Nov 1, 2015

You should be looking towards str.format() and end='' for your print statement

Here's a little example

for x in range(10):
    print("{:<10}".format("{:0.1f}".format(x)), end='')

The first part for the rounding to 1 decimal place for your float values is

"{:0.1f}".format(x)

The 0 is for no left padding, the following .1f is for rounding to 1 decimal place. Next is the main part

"{:<10}".format(...)

This will print with a minimum string length of 10 characters. So if you have a string such as Hello it is printed as Hello_____ ( _ to represent empty spaces) The 10 can be changed to a value of your choosing.

Finally the end=''. By default the end parameter is \n which is what creates the new line after each print, but by changing it you can form lines from multiple print statements. Just print normally or set end='\n' when you want to end the line.

Little something to note as well. When checking if a value is between two integers you can use this instead of checking it twice (also you would need and and operator instead of or )

elif 60 <= score <= 69: