To find the average and grade for given marks
Expt. No.01
Page No.2
Average and grade
Aim
To write a Python program that accepts marks in five subjects, finds the total and average, and displays the grade.
Concepts used
Algorithm
- Input the marks of five subjects.
- Total = sum of the marks; average = total ÷ 5.
- Grade: 90 and above A, 75 and above B, 60 and above C, 33 and above D, below 33 E.
- Display the total, average and grade.
Program
# Average and grade for marks in five subjects eng = float(input("English : "))maths = float(input("Maths : "))phy = float(input("Physics : "))chem = float(input("Chemistry : "))ip = float(input("Inf. Pract.: ")) total = eng + maths + phy + chem + ipaverage = total / 5if average >= 90: grade = "A"elif average >= 75: grade = "B"elif average >= 60: grade = "C"elif average >= 33: grade = "D"else: grade = "E"print("Total :", total)print("Average:", average)print("Grade :", grade)
A marked line continues the line above it. In your file, write it on the same line.
Output
$ python average_and_grade.py
English : 78
Maths : 85
Physics : 69
Chemistry : 72
Inf. Pract.: 94
Total : 398.0
Average: 79.6
Grade : B
✓ Printed by running this program, not typed by hand.
Result
The marks 78, 85, 69, 72 and 94 gave a total of 398.0 and an average of 79.6, so the grade was B.
Viva voce: what the examiner may ask
01Why is float() used and not int()?
Marks can have decimals, such as 72.5; int() would fail on them.
02Why is the order of the conditions important?
The first true condition wins, so the highest range must be checked first.
03What is the data type of total?
float, because the marks were converted with float().
Variations for the lab test
- Store the five marks in a list and find the total with
sum(). - Display "Pass" or "Fail" (fail if any subject is below 33).
- Display the percentage if the maximum marks per subject is 80.
! Common mistake Writing
average = total / 5 before the marks are input gives a NameError, because total doesn't exist yet.