determining average grades and displaying letter grades - python

I'm working on a program for class that finds the average of 5 entered test scores then displays the letter grades relevant to each letter score. letter score is a 10 point system ( A = 90-100 B = 80-89, etc)
This is what I've put together so far but it doesn't seem to recognize "avg" in the syntax. any suggestions?
def main():
while true:
grade = int(input('Enter grade: '))
total += grade
avg = calc_average(total)
abc_grade = determine_grade(grade)
print('Average grade is: ' avg)
print('Letter grades for entered grades are: ' abc_grade)
def calc_average(total):
return total / 5
def determine_grade(grade):
if grade >= 90 and <= 100:
return 'A'
elif grade >= 80 and <= 89:
return 'B'
elif grade >= 70 and <= 79:
return 'C'
elif grade >= 60 and <= 69:
return 'D'
else:
return 'F'
main()

use:
print('Average grade is: '+str(avg))
print('Letter grades for entered grades are: '+abc_grade)
or
print('Average grade is: %.2f'%(avg))
print('Letter grades for entered grades are: %s'%(abc_grade))

_list = []
def calc_average(total):
return total / 5
def determine_grade(grade):
if grade >= 90 and grade <= 100:
return 'A'
elif grade >= 80 and grade <= 89:
return 'B'
elif grade >= 70 and grade <= 79:
return 'C'
elif grade >= 60 and grade <= 69:
return 'D'
else:
return 'F'
while True:
grade = int(input('Enter grade: '))
_list.append(grade)
avg = calc_average(sum(_list))
abc_grade = ' '.join([determine_grade(mark) for mark in _list])
if len(_list) > 5:
break
print('Average grade is: ', avg)
print('Letter grades for entered grades are: ', abc_grade)

def main():
print("This is a program which displays the grade from a score")
print("")
grade = eval(input("What is the value of the score : "))
print("")
if 90 <= grade <= 100:
print("Your get an A")
elif 80 <= grade <= 89:
print("Your get a B")
elif 70 <= grade <= 79:
print("Your get a C")
elif 60 <= grade <= 69:
print("Your get a D")
else:
print("Your get an F")
main()

This Worked for me.. A few minor changes except your code is working fine.
def main():
total = 0;avg = 0;abc_grade = 0
def calc_average(total):
return total / 5
def determine_grade(grade):
if 90 <= grade <= 100:
return 'A'
elif 80 <= grade <= 89:
return 'B'
elif 70 <= grade <= 79:
return 'C'
elif 60 <= grade <= 69:
return 'D'
else:
return 'F'
while(True):
grade = int(input('Enter grade: '))
total += grade
avg = calc_average(total)
abc_grade = determine_grade(grade)
print('Average grade is: ' + str(avg))
print('Letter grades for entered grades are: ' + str(abc_grade))
main()
Hope you can find out differences. :)

Related

Program to calculate grade

I am writing a python program to calculate the test grade of 5 students and it will show what grade they get by entering their test score. But I can't get a result like this. Can someone help me out, please!
5
88
92
76
100
66
Grade Distribution
A: 2
B: 1
C: 1
D: 1
F: 0
Average Grade: 84.4
`def read_test_scores():
print("Enter number of students: ")
num = int(input())
print("Enter the next grade: ")
score1 = int(input())
score2 = int(input())
score3 = int(input())
score4 = int(input())
score5 = int(input())
sum = (score)
tavge = sum / 5.0
return tavge
def get_letter_grade():
if 90 <= n <= 100:
grade = 'A'
elif 80 <= n <= 89:
grade = 'B'
elif 70 <= n <= 79:
grade = 'C'
elif 60 <= n <= 69:
grade = 'D'
elif n<60:
grade = 'F'
return grade
def print_comment(grade):
a=0
b=0
c=0
d=0
f=0
if grade == 'A':
a += 1
print ('A: ' +str(a))
elif grade == 'B':
b += 1
print ('B: ' +str(b))
elif grade == 'C':
c += 1
print ('C: ' +str(c))
elif grade == 'D':
d +=1
print ('D: ' +str(d))
elif grade == 'F':
f +=1
print ('F: ' +str(f))
tavge = read_test_scores()
print ('Grade Distribution')
print_comment(grade)
print ("Test Average is: " + str(tavge))`
Here's what I think you were going for. I had to make several changes that I can clarify if needed... Mostly you needed to store the scores and grades as an array of values instead of individual ones. When you need to, for instance, tally up the number of A's, B's, etc. you just loop through the array and add up the occurrences.
def read_test_scores():
print("Enter number of students: ")
num = int(input())
scores = []
print("Enter the next grade: ")
for score in range(num):
score = int(input())
while score < 0 or score > 100:
print('Score should be between 0 and 100, try again:')
score = int(input())
scores.append(score)
sum_scores = sum(scores)
tavge = sum_scores / num
return tavge, scores
def get_letter_grade(n):
if 90 <= n <= 100:
grade = 'A'
elif 80 <= n <= 89:
grade = 'B'
elif 70 <= n <= 79:
grade = 'C'
elif 60 <= n <= 69:
grade = 'D'
elif n<60:
grade = 'F'
return grade
def print_comment(grades):
a=0
b=0
c=0
d=0
f=0
for grade in grades:
if grade == 'A':
a += 1
elif grade == 'B':
b += 1
elif grade == 'C':
c += 1
elif grade == 'D':
d +=1
elif grade == 'F':
f +=1
print ('A: ' +str(a))
print ('B: ' +str(b))
print ('C: ' +str(c))
print ('D: ' +str(d))
print ('F: ' +str(f))
tavge, scores = read_test_scores()
grades = []
for score in scores:
grades.append(get_letter_grade(score))
print ('Grade Distribution')
print_comment(grades)
print ("Test Average is: " + str(tavge))
This is my idea
Get number of students
Create total_score, or any Something you need to record
for in Students
Get score then add in total_score
total_score = 0
for i in range(student_number) :
score = int(input())
total_score += score
# get_letter_grade
average = total_score / student_number
print ("Test Average is" + str())

I'm struggling to make the function work on my code. I'm missing the part where i want to print the letter grade

Write a program that asks the user to enter five test scores.
Assume valid scores will be entered and each number will be entered separately, i.e. you will need 5 variables. The program should display a letter grade for each score and the average test score. Write the following functions in the program:
main - asks the user to enter five test scores separately, placing them into five float variables. main should then call showScores 5 times passing one of each of the scores each time. When returned from showScores, main should then call calcAverage passing it the 5 scores.
showScores – receives a single score and prints the score to the console (without starting a new line) and sends the score just printed to printLetterGrade which will print a letter grade on the same line.
printLetterGrade - accepts a single number as an argument and displays a letter grade for the score
based on the following grading scale:
Score Letter Grade
90-100 A
80-89 B
70-79 C
60-69 D
Below 60 F
 calcAverage - receives the 5 scores as arguments and displays the average of the scores, along with a letter grade equivalent to that average (take advantage of the function printLetterGrade to display the letter by passing it the calculated average).
Here is a sample run of the program:
Enter grade 1: 65
Enter grade 2: 80
Enter grade 3: 90
Enter grade 4: 71
Enter grade 5: 85
65 is D
80 is B
90 is A
71 is C
85 is B
The average is: 78.2 which is C
this is what i did so far but im struggling on some parts so that i get the output shown. here's what i have done so far.
def main():
grade1 = float(input("Enter grade 1:"))
grade2 = float(input("Enter grade 2:"))
grade3 = float(input("Enter grade 3:"))
grade4 = float(input("Enter grade 4:"))
grade5 = float(input("Enter grade 5:"))
showScores(grade1, grade2, grade3, grade4, grade5)
calcAverage(grade1, grade2, grade3, grade4, grade5)
def showScores(grade1, grade2, grade3, grade4, grade5):
print(grade1)printLetterGrade
print(grade2)printLetterGrade
print(grade3)printLetterGrade
print(grade4)printLetterGrade
print(grade5)printLetterGrade
def printLetterGrade(showScores):
if(grade < 60):
return printLetterGrade == "F"
elif(grade < 70):
return printLetterGrade == "D"
elif(grade < 80):
return printLetterGrade == "C"
elif(grade < 90):
return printLetterGrade == "B"
elif(grade < 101):
return printLetterGrade == "A"
return printLetterGrade
def calcAverage(grade1, grade2, grade3, grade4, grade5):
average = (grade1 + grade2 + grade3 + grade4 + grade5)/ 5
print("The average is {}".format(average))
main()
You need to adjust your showScores and printLetterGrade functions as follows:
def showScores(grade1, grade2, grade3, grade4, grade5):
print("{} is {}".format(grade1, printLetterGrade(grade1)))
print("{} is {}".format(grade2, printLetterGrade(grade2)))
print("{} is {}".format(grade3, printLetterGrade(grade3)))
print("{} is {}".format(grade4, printLetterGrade(grade4)))
print("{} is {}".format(grade5, printLetterGrade(grade5)))
def printLetterGrade(grade):
if (grade < 60):
printLetterGrade = "F"
elif (grade < 70):
printLetterGrade ="D"
elif (grade < 80):
printLetterGrade = "C"
elif (grade < 90):
printLetterGrade = "B"
elif (grade < 101):
printLetterGrade = "A"
return printLetterGrade

How can I make it so the user only has 2 tries to enter valid input?

print('Hello, welcome to your grade calculator.')
GradeCount = 0
totalGrades = 0.0
moreStudent = 'y'
while moreStudent == 'y' or moreStudent == 'Y':
grade = float(input('Enter a grade or a -1 to end: '))
while grade != -1:
if grade > 100 or grade < 0:
print('Invalid input. Please enter a value between 1 and 100.')
grade = float(input('Enter the next grade or -1 to end: '))
continue
totalGrades = totalGrades + grade
GradeCount = GradeCount + 1
if 90 <= grade <=100:
print('You got an A. Thats awesome.')
print('Number of grades entered: ',GradeCount)
print('Class total: ',totalGrades)
elif 80 <= grade < 90:
print('You got a B. Good job.')
print('Number of grades entered: ',GradeCount)
print('Class total: ',totalGrades)
elif 70 <= grade < 80:
print('You got a C. Thats fine I guess.')
print('Number of grades entered: ',GradeCount)
print('Class total: ',totalGrades)
elif 60 <= grade < 70:
print ('You got a D. Not very good.')
print('Number of grades entered: ',GradeCount)
print('Class total: ',totalGrades)
elif grade < 60:
print ('You got an F. You fail.')
print('Number of grades entered: ',GradeCount)
print('Class total: ',totalGrades)
grade = float(input('Enter the next grade or -1 to end: '))
moreStudent = input('Are you a new student and ready to enter your
grades? y or n: ')
print ('Number of grades entered:', GradeCount)
print ('Class total:',totalGrades)
print ('Class grade average:', format(totalGrades / GradeCount, '.2f'))
How can I make it so the user only has 2 tries before the program issues an error message then clears the screen and starts over? Also how can I make it clear the screen every time there is a new user?
The most basic modification you could use to the current code is to add a counter, other methods can be used as well
while moreStudent == 'y' or moreStudent == 'Y':
grade = float(input('Enter a grade or a -1 to end: '))
count = 0
while grade != -1:
if grade > 100 or grade < 0:
count += 1
if count == 2:
moreStudnet = 'n'
break
else:
print('Invalid input. Please enter a value between 1 and 100.')
grade = float(input('Enter the next grade or -1 to end: '))
continue
totalGrades = totalGrades + grade
GradeCount = GradeCount + 1

Python program to calculate student grades [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
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)
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.
there are many errors in your code, some of them are in the comment, but the most critical part is that you use global and local variables incorrectly
here is and example of fixing your code using the correct way to use global variables.
https://repl.it/repls/SorrowfulOddballSongbird
tavge = 0
exam = 0
sid = 0
final_score = 0
grade = ''
def read_test_scores() :
global sid
print("ENTER STUDENT ID: ")
sid = int(input())
global exam
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())
total = (score1 + score2 + score3 + score4 + score5 + score6 + score7)
global tavge
tavge = total/7
#return tavge
def compute_final_score() :
global final_score
final_score = 0.4 * tavge + 0.6 * exam
#return final_score
def get_letter_grade() :
global grade
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() :
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()
print ("LETTER GRADE IS: " + str(grade))
print_comment()
but you should consider using parameters instead using globals
As several people have mentioned you need to use == for comparison, you also are missing a colon after one of your if/else.
This is my take on your code. Keep in mind that this doesn't have and tests to make sure someone is actually entering in a number for a test score instead of text
"sum" is also the name of a built in function to Python, which sums up anything you provide it.
def read_test_scores():
scores = []
num_tests = 7
print("ENTER ALL TEST SCORES: ")
for i in range(num_tests):
score = input("Test " + str(i + 1) + ":")
scores.append(int(score))
return sum(scores) / num_tests
def compute_final_score(average, exam_score):
score = 0.4 * average + 0.6 * exam_score
return score
def get_letter_grade(finalized_score):
if 90 <= finalized_score <= 100:
letter_grade = 'A'
elif 80 <= finalized_score <= 89:
letter_grade = 'B'
elif 70 <= finalized_score <= 79:
letter_grade = 'C'
elif 60 <= finalized_score <= 69:
letter_grade = 'D'
else:
letter_grade = 'F'
return letter_grade
def print_comment(letter_grade):
if letter_grade == 'A':
print("COMMENT: Very Good")
elif letter_grade == 'B':
print("COMMENT: Good")
elif letter_grade == 'C':
print("COMMENT: Satisfactory")
elif letter_grade == 'D':
print("COMMENT: Need Improvement")
elif letter_grade == 'F':
print("COMMENT: Poor")
def get_student_id():
print("ENTER STUDENT ID: ")
identity = int(input())
return identity
def get_exam_score():
print("ENTER EXAM SCORE: ")
exam_score = int(input())
return exam_score
if __name__ == '__main__':
student_id = get_student_id()
exam = get_exam_score()
tavge = read_test_scores()
print("TEST AVERAGE IS: " + str(tavge))
final_score = compute_final_score(tavge, exam)
print("FINAL SCORE IS: " + str(final_score))
grade = get_letter_grade(final_score)
print("LETTER GRADE IS: " + str(grade))
print_comment(grade)

python programming- average of exams and assignments and print grade

Please im trying to Write a Python program that calculates a student Final grade for a class that has two assignments and two exams. Assignments worth 40% of the class grade and exams worth 60% of the class grade. The program should perform the following steps:
Ask the user for assignment1, assignment2, exam1, and exam2 grades. all grades are out of 100 .
Calculate the average of the assignments.
Calculate the average of the exams.
Calculate the final grade using the following formula:
final grade=.4*average of the assignments+.6*average of the exams.
Format the final grade to have 2 decimal places.
Display a message telling the student what the final grade is.
here is my program:
from math import *
def main():
Assignment1 = eval(input("Please enter the score for Assignment 1: "))
Assignment2 = eval(input("Please enter the score for Assignment 2: "))
Assignment_total = Assignment1 + Assignment2
Assignment_average = Assignment_total/2
print("The average of the assignment is", round(Assignment_average, 2))
Exam1 = eval(input("Please enter the score for Exam 1: "))
Exam2 = eval(input("Please enter the score for Exam 2: "))
Exam_total = Exam1 + Exam2
Exam_average = Exam_total/2
print("The average of the Exam is", round(Exam_average, 2))
Final_grade = 0.4 * Assignment_average + 0.6 * Exam_average
if 90 <= Final_grade <= 100:
return 'A'
elif 80 <= Final_grade <= 89:
return 'B'
elif 70 <= Final_grade <= 79:
return 'C'
elif 60 <= Final_grade <= 69:
return 'D'
else:
return 'F'
main()
i cannot get it to print The grades. please help me
You are all over the page here. You have return but you are not returning anything (i.e. you would have to call grade = main() and then print(grade).
Take a look at my comments below:
# nothing you are doing requires the math module
# eval() and round() are built-ins; we dont even need eval()
## from math import *
def main():
# variable names = short & sweet + meaningful
a1 = int(input("Please enter the score for Assignment 1: "))
a2 = int(input("Please enter the score for Assignment 2: "))
atot = a1 + a2
aavg = atot / 2
print ("The average of the assignment is", round(aavg, 2))
e1 = int(input("Please enter the score for Exam 1: "))
e2 = int(input("Please enter the score for Exam 2: "))
etot = e1 + e2
eavg = etot / 2
print ("The average of the Exam is", round(eavg, 2))
fingrd = ((0.4 * aavg) + (0.6 * eavg))
if (90 <= fingrd <= 100):
print (fingrd, ': A') # edit: included print format you commented
# no need to do <= on the upper bounds
# < upper_bound includes <= your previous lower_bound - 1
# i.e. 80 <= fingrd < 90 equates to 80 <= fingrd < 90
elif (80 <= fingrd < 90):
print (fingrd, ': B')
elif (70 <= fingrd < 80):
print (fingrd, ': C')
elif (60 <= fingrd < 70):
print (fingrd, ': D')
else:
print (fingrd, ': F')
if __name__ == '__main__':
main()
Output:
Please enter the score for Assignment 1: 70
Please enter the score for Assignment 2: 100
The average of the assignment is 85.0
Please enter the score for Exam 1: 45
Please enter the score for Exam 2: 87
The average of the Exam is 66.0
73.6 : C
Taking my suggestion and #Toad22222's and also getting rid of that scary eval:
from math import *
def main():
Assignment1 = int(input("Please enter the score for Assignment 1: "))
Assignment2 = int(input("Please enter the score for Assignment 2: "))
Assignment_total = Assignment1 + Assignment2
Assignment_average = Assignment_total/2
print("The average of the assignment is", round(Assignment_average, 2))
Exam1 = int(input("Please enter the score for Exam 1: "))
Exam2 = int(input("Please enter the score for Exam 2: "))
Exam_total = Exam1 + Exam2
Exam_average = Exam_total/2
print("The average of the Exam is", round(Exam_average, 2))
Final_grade = 0.4 * Assignment_average + 0.6 * Exam_average
print("The final grade is", round(Final_grade, 2))
if 90 <= Final_grade <= 100:
print('A')
elif 80 <= Final_grade <= 89:
print('B')
elif 70 <= Final_grade <= 79:
print('C')
elif 60 <= Final_grade <= 69:
print('D')
else:
print('F')
main()
UPDATE
Kind of a rewrite just for fun. Feel free to ignore or take inspiration or ask questions.
import collections
Component = collections.namedtuple("Component", ["name", "count", "weight"])
def get_average(name, how_many):
return sum(
int(input("Please enter the score for {} {}: ".format(name, i+1)))
for i in range(how_many)
) / how_many
def main():
components = [
Component(name="assignment", count=2, weight=0.4),
Component(name="exam", count=2, weight=0.6),
]
total = 0
for component in components:
average = get_average(component.name, component.count)
print("The average of the {}s is: {:.2f}".format(component.name, average))
print()
total += average * component.weight
letters = [(90, "A"), (80, "B"), (70, "C"), (60, "D")]
grade = next((letter for score, letter in letters if total >= score), "F")
print("The final grade is: {:.2f} ({})".format(total, grade))
main()
You need to call the function by assigning it to a variable to then print. See below:
from math import *
def main():
Assignment1 = eval(input("Please enter the score for Assignment 1: "))
Assignment2 = eval(input("Please enter the score for Assignment 2: "))
Assignment_total = Assignment1 + Assignment2
Assignment_average = Assignment_total/2
print("The average of the assignment is", round(Assignment_average, 2))
Exam1 = eval(input("Please enter the score for Exam 1: "))
Exam2 = eval(input("Please enter the score for Exam 2: "))
Exam_total = Exam1 + Exam2
Exam_average = Exam_total/2
print("The average of the Exam is", round(Exam_average, 2))
Final_grade = round(0.4 * Assignment_average + 0.6 * Exam_average)
if 90 <= Final_grade <= 100:
return "Your final grade is %s: A" %(Final_grade)
elif 80 <= Final_grade <= 89:
return "Your final grade is %s: B" %(Final_grade)
elif 70 <= Final_grade <= 79:
return "Your final grade is %s: C" %(Final_grade)
elif 60 <= Final_grade <= 69:
return "Your final grade is %s: D" %(Final_grade)
else:
return "Your final grade is %s: F" %(Final_grade)
mygrades = main()
print (mygrades)
Output
Please enter the score for Assignment 1: 43
Please enter the score for Assignment 2: 88
The average of the assignment is 65.5
Please enter the score for Exam 1: 90
Please enter the score for Exam 2: 89
The average of the Exam is 89.5
Your final grade is 80: B

Categories

Resources