Q. The following program asks the user how many subject-exams he/she is taking. Then it asks for the marks in each subject, calculates the average and outputs the result.


Given below is incomplete code for the same. Complete the code by writing missing functionality.


'''
Return the average or mean mark for a num_subjects.
precondition : num_subjects > 0
postcondition : computed average
'''

num_subjects = int(input ( "How many subjects are you taking?" ))
#initialize the accumulator for the total number of marks
sum = _____

#obtain each mark from the user and keep a running total

for index in _____ (num_subjects) _____
	prompt = "Enter marks for subject #" + str(index + 1) + ":"
	marks = _____ ( input ( _______ ))
	_____ = ________  #add marks to total

#calculate average based on average total marks / number of marks
average = _____ / _____
print ("Your average marks in", _____, "subjects are", _____ )


Answer :-

num_subjects = int(input ( "How many subjects are you taking? :- " ))
sum = 0
for index in range (num_subjects) :
    marks = int( input ( "Enter marks for subject " + str(index + 1) + ":- " ))
    sum = sum + marks

average = sum / num_subjects
print ("Your average marks in", num_subjects, "subjects are", average )


Output :-

How many subjects are you taking? :- 5
Enter marks for subject 1:- 59
Enter marks for subject 2:- 36
Enter marks for subject 3:- 98
Enter marks for subject 4:- 67
Enter marks for subject 5:- 79
Your average marks in 5 subjects are 67.8

>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post