2

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

6
  • 1
    I think you should start with correcting determine_grade: use >= instead of => and use and instead of or. Commented Nov 1, 2015 at 0:41
  • 1
    Use the idea of tabstops—en.wikipedia.org/wiki/Tab_stop Commented Nov 1, 2015 at 1:11
  • 1
    The example Arun Das gave was in Python 2 syntax. To fix it, move the first closing bracket ) to the end of the line, after the 2nd closing bracket Commented Nov 1, 2015 at 6:05
  • Made the fix, Steven Summers, but now it says "TypeError: not all arguments converted during string formatting" Commented Nov 1, 2015 at 16:17
  • Hey everybody, I'm almost finished but need help with a couple of things. See the stuff under Hopefully my final edit: Commented Nov 1, 2015 at 23:37

3 Answers 3

1

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

Comments

1

You could do something like this:

def display_menu():
print "Name\t\t\tnumeric grade\t\tlettergrade"
print "---------------------------------------------------------------"
print "%s:\t\t\t%f\t\t%s" % ('name1', 50, 'F')
print "%s:\t\t\t%f\t\t%s" % ('name2', 50, 'F')
print "%s:\t\t\t%f\t\t%s" % ('name3', 23, 'F')
print "%s:\t\t\t%f\t\t%s" % ('name4', 44, 'F')
print "%s:\t\t\t%f\t\t%s" % ('name5', 48, 'F')
print "---------------------------------------------------------------"

display_menu()

I used \t to give tabs in between.

This is the screenshot of the PythonShell Output

3 Comments

It said "invalid syntax" after the "%s:\t\t\t%f\t\t%s", and indicated the quotation mark at the end. I'm using Python 3.4.3., if that helps.
Sorry I am using v2.7. That is why it didn't work for you. Let me just run a quick check.
It's alright, Arun. I figured out the syntax. I'm almost on my way to a solution, but still need help with a couple of things.
0

Well, I just got the solution back, and I did a real butchering on the code. But, I will post this so no one will have to suffer like I did:

# main function
    def main():
        # Local variables only have to define floats
        average = 0.0
        score1 = 0.0
        score2 = 0.0
        score3 = 0.0
        score4 = 0.0
        score5 = 0.0

        # Get scores
        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:')

        # Calculate average grade
        average = calculate_average(score1, score2, score3, score4, score5)

        #Display grade and average information in tabular form
        print('Name\t\tnumeric grade\tletter grade')
        print('----------------------------------------------------')
        print(name1 + ':\t\t', score1, '\t\t', determine_grade(score1))
        print(name2 + ':\t\t', score2, '\t\t', determine_grade(score2))
        print(name3 + ':\t\t', score3, '\t\t', determine_grade(score3))
        print(name4 + ':\t\t', score4, '\t\t', determine_grade(score4))
        print(name5 + ':\t\t', score5, '\t\t', determine_grade(score5))
        print('----------------------------------------------------')
        print ('Average score:\t', average, '\t\t', \
               determine_grade(average))

    # The calc_average function returns average of 5 grades 
    def calculate_average(s1, s2, s3, s4, s5):
        return  (s1 + s2 + s3 + s4 + s5) / 5.0

    # The determine_grade function receives a numeric  
    # grade and returns the corresponding letter grade 
    def determine_grade(score):
        if score >= 90:
            return 'A'
        elif score >= 80:
            return 'B'
        elif score >= 70:
            return 'C'
        elif score >= 60:
            return 'D'
        else:
            return 'F'

    # Call the main function.
    main()

Comments

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.