multipleQuestions
newQuestion Write a program that accepts marks in 4 subjects, computes total, average, and grade using if-else.
newAnswer
# Program using if-else
def calculate_grade(marks):
total = sum(marks)
average = total / len(marks)
if average >= 70:
grade = 'A'
remark = 'Excellent'
elif average >= 60:
grade = 'B'
remark = 'Good'
elif average >= 50:
grade = 'C'
remark = 'Satisfactory'
elif average >= 40:
grade = 'D'
remark = 'Pass'
else:
grade = 'F'
remark = 'Fail'
return total, average, grade, remark
marks = [float(input("Enter marks for subject {}: ".format(i+1))) for i in range(4)]
total, average, grade, remark = calculate_grade(marks)
print(f"Total: {total}, Average: {average}, Grade: {grade}, Remark: {remark}")
newQuestion Write a program that accepts marks in 4 subjects, computes total, average, and grade using switch-case.
newAnswer
# Program using switch-case (simulated with dictionary)
def calculate_grade(marks):
total = sum(marks)
average = total / len(marks)
def get_grade_remark(avg):
switcher = {
(70, 100): ('A', 'Excellent'),
(60, 69): ('B', 'Good'),
(50, 59): ('C', 'Satisfactory'),
(40, 49): ('D', 'Pass'),
(0, 39): ('F', 'Fail'),
}
for key in switcher:
if key[0] <= avg <= key[1]:
return switcher[key]
grade, remark = get_grade_remark(average)
return total, average, grade, remark
marks = [float(input("Enter marks for subject {}: ".format(i+1))) for i in range(4)]
total, average, grade, remark = calculate_grade(marks)
print(f"Total: {total}, Average: {average}, Grade: {grade}, Remark: {remark}")