Answer to Question #197871 in Python for LEROY

Question #197871

 Write a program ( using loop ) that reads five marks and computes the average. Use the sample code from today’s lesson on adding three numbers to help you get started. 

 

b) Modify the program to also output the lowest and highest mark. 

 

c) Modify the program to check if the mark entered is between 0 and 100. 

 

d) Modify the program to ask the user to enter -1 when done entering marks. 

 

e) Write the same program using Try and except statement  



1
Expert's answer
2021-05-26T07:24:04-0400
print('Please enter marks in range [0;100] in one line separated by space.')
print('Enter -1 when done entering marks:')
numbers = [int(i) for i in input().split()]

try: 
    marks = []
    for i in numbers:
        if i == -1:
            break
        elif i < 0 or i > 100:
            raise ValueError('The mark', i, 'is not in range [0;100]')
        else:
            marks.append(i)

    if len(marks) == 0:
        raise ValueError('No marks entered')

    min_mark = marks[0]    
    max_mark = marks[0]    
    sum = 0
    for i in marks:
        sum += i
        min_mark = min_mark if i > min_mark else i
        max_mark = max_mark if i < max_mark else i

    print('average mark:', sum/len(marks))
    print('lowest mark: ', min_mark)
    print('highest mark:', max_mark)

except ValueError as err:
    print('Error: ', err)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS