Skip to content
Advertisement

Python program to calculate student grades [closed]

This is my first program in Python and I am having some trouble so forgive me if I have some simply syntax issues.

I am writing a program that calculates a student’s final score based on one exam grade worth 60% and 7 other test scores worth a combined 40% of the final grade. The user is asked to input the one exam score then asked to input the 7 test scores which are read in a loop. The letter grade is then determined from the final score calculated from the exam and tests. After that a grade comment is printed corresponding to the letter grade given to the student. This is my code so far:

def read_test_scores() :

    print("ENTER STUDENT ID: ")
    id = int(input())

    print("ENTER EXAM SCORE: ")
    exam = int(input())

    print("ENTER ALL TEST SCORES: ")
    score1 = int(input())
    score2 = int(input())
    score3 = int(input())
    score4 = int(input())
    score5 = int(input())
    score6 = int(input())
    score7 = int(input())

    sum = (score1 + score2 + score3 + score4 + score5 + score6 + score7)

    tavge = sum/7
    return tavge

def compute_final_score(tavge, exam) :
    final_score = 0.4 * tavge + 0.6 * exam
    return final_score

def get_letter_grade(final_score) :

    if 90 <= final_score <= 100:
        grade = 'A'
    elif 80 <= final_score <= 89:
        grade = 'B'
    elif 70 <= final_score <= 79:
        grade = 'C'
    elif 60 <= final_score <= 69:
        grade = 'D'
    else:
        grade = 'F'
    return grade

def print_comment(grade) :

    if grade = 'A':
        print "COMMENT:            Very Good"
    elif grade = 'B':
        print "COMMENT:            Good"
    elif grade = 'C':
        print "COMMENT:            Satisfactory"
    elif grade = 'D':
        print "COMMENT:            Need Improvement"
    elif grade = 'F'
        print "COMMENT:            Poor"

read_test_scores()
print "TEST AVERAGE IS:    " + str(tavge)
compute_final_score()
print "FINAL SCORE IS:     " + str(final_score)
get_letter_grade(final_score)
print "LETTER GRADE IS:     " + str(grade)
print_comment(grade)

Advertisement

Answer

Here’s my answer. The code should run. Notes are inserted as comments.

# NOTE: I haven't checked whether your math is right, or
# if the computed values are correct.  I did however get your
# script to work.


def read_test_scores():
    print("ENTER STUDENT ID: ")
    id = int(input())

    print("ENTER EXAM SCORE: ")
    exam = int(input())

    print("ENTER ALL TEST SCORES: ")
    score1 = int(input())
    score2 = int(input())
    score3 = int(input())
    score4 = int(input())
    score5 = int(input())
    score6 = int(input())
    score7 = int(input())

    sum = (score1 + score2 + score3 + score4 + score5 + score6 + score7)

    tavge = sum / 7.0

    # NOTE: if you want to use any variables from this function,
    # then you have to "bring them outside" by "returning"
    # them. Here, I return the values tavge, id, and exam. I noticed
    # that bringing out "exam" is necessary since you'll
    # be using it later on.
    return tavge, id, exam


def compute_final_score(tavge, exam):
    final_score = 0.4 * tavge + 0.6 * exam
    return final_score


def get_letter_grade(final_score):
    if 90 <= final_score <= 100:
        grade = 'A'
    elif 80 <= final_score <= 89:
        grade = 'B'
    elif 70 <= final_score <= 79:
        grade = 'C'
    elif 60 <= final_score <= 69:
        grade = 'D'
    else:
        grade = 'F'
    return grade


def print_comment(grade):
    # NOTE `=` is for assignment.  We use it when we want to
    # tell python to make a variable mean something. For example:
    # a = "some_name" basically means that when we call a, it would
    # return the string "some_name".

    # What you want to use here is `==` which is the equality operator.
    # This checks whether or thing are equal.
    if grade == 'A':
        print("COMMENT:            Very Good")
    elif grade == 'B':
        print("COMMENT:            Good")
    elif grade == 'C':
        print("COMMENT:            Satisfactory")
    elif grade == 'D':
        print("COMMENT:            Need Improvement")
    elif grade == 'F':
        print("COMMENT:            Poor")


# NOTE 1: you need to assign the function results to a
# variable (or variables), otherwise, the result or return value
# will go nowhere and you can't use it
tavge, id, exam = read_test_scores()
print "TEST AVERAGE IS:    " + str(tavge)

# NOTE 2: variable names do not have to be the same as
# the name in their respective functions.  Here, you can see
# that it will still run even if I changed the variable
# name final_score to my_variable.  Although, of course, using
# final_score would still work.

# NOTE 3: the final_score function requires 2 inputs,
# namely tavge and exam.  This basically means that you have to feed
# it with these 2 values for it to work.  I took the
# tavge and exam variables as the results from your read_test_scores
# function
my_variable = compute_final_score(tavge, exam)
print("FINAL SCORE IS:     " + str(my_variable))
grade = get_letter_grade(my_variable)
print("LETTER GRADE IS:     " + str(grade))
print_comment(grade)


# FINAL NOTE: I haven't commented regarding coding style etc (like say
# for instance, there are best practices regarding variable names
# within functions, that is, if they should be similar to variable names
# outside the function), but regardless, the code is a good start.  I
# would also advise you to try to narrow down your question first
# before posting.  This can be done by running your code, and searching
# the internet for the particular erro messages, and if you're still stuck,
# ask here on stackoverflow.
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement