In: Computer Science
3. Write a program that will accept only 5 numbers from 50 to 100. The program should remind the user if an inputted number is not on the range. Compute the sum and average of the 1st and the 5th inputted numbers.
Here is the code using the Python 3 programming language for the given question:
Afte code are the screenshots attached for the proper understanding of the indentation and output of the complied code is also attached.
Explanation of the code is provided using the comments in the code.
In this code, Custom exception has been created.
class Error(Exception):
"""Base class for other exceptions"""
pass
class ValueError(Error):
"""Raised when the input value is too small"""
pass
while True:
nums = list()
#list for storing values
for i in range(1,6):
#taking 5 inputs
number = input("Enter Number between 50 and 100: ")
#using exception handling to check number
try:
number = int(number)
if number > 50 and number < 101:
nums.append(number)
else:
#raising the exception
raise ValueError
except ValueError:
print("Number not in range")
continue
if len(nums)==5:
#slicing the list to get the values
a = nums[0]
b = nums[-1]
#printing sum and avgerage
print("Sum of 1st and 5th Numbers is ",a+b)
print("Average of 1st and 5th Number is ",((a+b)/2))
break