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:
JavaScript
x
61
61
1
def read_test_scores() :
2
3
print("ENTER STUDENT ID: ")
4
id = int(input())
5
6
print("ENTER EXAM SCORE: ")
7
exam = int(input())
8
9
print("ENTER ALL TEST SCORES: ")
10
score1 = int(input())
11
score2 = int(input())
12
score3 = int(input())
13
score4 = int(input())
14
score5 = int(input())
15
score6 = int(input())
16
score7 = int(input())
17
18
sum = (score1 + score2 + score3 + score4 + score5 + score6 + score7)
19
20
tavge = sum/7
21
return tavge
22
23
def compute_final_score(tavge, exam) :
24
final_score = 0.4 * tavge + 0.6 * exam
25
return final_score
26
27
def get_letter_grade(final_score) :
28
29
if 90 <= final_score <= 100:
30
grade = 'A'
31
elif 80 <= final_score <= 89:
32
grade = 'B'
33
elif 70 <= final_score <= 79:
34
grade = 'C'
35
elif 60 <= final_score <= 69:
36
grade = 'D'
37
else:
38
grade = 'F'
39
return grade
40
41
def print_comment(grade) :
42
43
if grade = 'A':
44
print "COMMENT: Very Good"
45
elif grade = 'B':
46
print "COMMENT: Good"
47
elif grade = 'C':
48
print "COMMENT: Satisfactory"
49
elif grade = 'D':
50
print "COMMENT: Need Improvement"
51
elif grade = 'F'
52
print "COMMENT: Poor"
53
54
read_test_scores()
55
print "TEST AVERAGE IS: " + str(tavge)
56
compute_final_score()
57
print "FINAL SCORE IS: " + str(final_score)
58
get_letter_grade(final_score)
59
print "LETTER GRADE IS: " + str(grade)
60
print_comment(grade)
61
Advertisement
Answer
Here’s my answer. The code should run. Notes are inserted as comments.
JavaScript
1
105
105
1
# NOTE: I haven't checked whether your math is right, or
2
# if the computed values are correct. I did however get your
3
# script to work.
4
5
6
def read_test_scores():
7
print("ENTER STUDENT ID: ")
8
id = int(input())
9
10
print("ENTER EXAM SCORE: ")
11
exam = int(input())
12
13
print("ENTER ALL TEST SCORES: ")
14
score1 = int(input())
15
score2 = int(input())
16
score3 = int(input())
17
score4 = int(input())
18
score5 = int(input())
19
score6 = int(input())
20
score7 = int(input())
21
22
sum = (score1 + score2 + score3 + score4 + score5 + score6 + score7)
23
24
tavge = sum / 7.0
25
26
# NOTE: if you want to use any variables from this function,
27
# then you have to "bring them outside" by "returning"
28
# them. Here, I return the values tavge, id, and exam. I noticed
29
# that bringing out "exam" is necessary since you'll
30
# be using it later on.
31
return tavge, id, exam
32
33
34
def compute_final_score(tavge, exam):
35
final_score = 0.4 * tavge + 0.6 * exam
36
return final_score
37
38
39
def get_letter_grade(final_score):
40
if 90 <= final_score <= 100:
41
grade = 'A'
42
elif 80 <= final_score <= 89:
43
grade = 'B'
44
elif 70 <= final_score <= 79:
45
grade = 'C'
46
elif 60 <= final_score <= 69:
47
grade = 'D'
48
else:
49
grade = 'F'
50
return grade
51
52
53
def print_comment(grade):
54
# NOTE `=` is for assignment. We use it when we want to
55
# tell python to make a variable mean something. For example:
56
# a = "some_name" basically means that when we call a, it would
57
# return the string "some_name".
58
59
# What you want to use here is `==` which is the equality operator.
60
# This checks whether or thing are equal.
61
if grade == 'A':
62
print("COMMENT: Very Good")
63
elif grade == 'B':
64
print("COMMENT: Good")
65
elif grade == 'C':
66
print("COMMENT: Satisfactory")
67
elif grade == 'D':
68
print("COMMENT: Need Improvement")
69
elif grade == 'F':
70
print("COMMENT: Poor")
71
72
73
# NOTE 1: you need to assign the function results to a
74
# variable (or variables), otherwise, the result or return value
75
# will go nowhere and you can't use it
76
tavge, id, exam = read_test_scores()
77
print "TEST AVERAGE IS: " + str(tavge)
78
79
# NOTE 2: variable names do not have to be the same as
80
# the name in their respective functions. Here, you can see
81
# that it will still run even if I changed the variable
82
# name final_score to my_variable. Although, of course, using
83
# final_score would still work.
84
85
# NOTE 3: the final_score function requires 2 inputs,
86
# namely tavge and exam. This basically means that you have to feed
87
# it with these 2 values for it to work. I took the
88
# tavge and exam variables as the results from your read_test_scores
89
# function
90
my_variable = compute_final_score(tavge, exam)
91
print("FINAL SCORE IS: " + str(my_variable))
92
grade = get_letter_grade(my_variable)
93
print("LETTER GRADE IS: " + str(grade))
94
print_comment(grade)
95
96
97
# FINAL NOTE: I haven't commented regarding coding style etc (like say
98
# for instance, there are best practices regarding variable names
99
# within functions, that is, if they should be similar to variable names
100
# outside the function), but regardless, the code is a good start. I
101
# would also advise you to try to narrow down your question first
102
# before posting. This can be done by running your code, and searching
103
# the internet for the particular erro messages, and if you're still stuck,
104
# ask here on stackoverflow.
105