Answer:
def Average(num):
if num == 0:
return 0
val = 0
trueNum = num
for i in range(0, num):
try:
val += int(input("Enter value (%d out of %d): " % (i+1,num)))
except Exception as e:
print ("Error processing value. Non integer detected.")
try:
val += int(input("Enter value (%d out of %d): " % (i+1,num)))
except Exception as e:
print ("Error processing value. Non integer detected.")
print ("OMITTING value from average.")
trueNum -= 1
return val/trueNum
def main():
try:
num = int(input("Enter a value N for amount of items: "))
if num < 0:
raise(ValueError)
except ValueError:
print ("N must be positive integer.")
exit(1)
print("Average: ", Average(num))
exit(0)
if __name__ == "__main__":
main()
Explanation:
This program is written in Python to collect some integer value from the user as an upper bound of integers to be input for an average. Using this upper bound, the program checks to validate it is indeed an integer. If it not an integer, then the program alerts the user and terminates. If it is a user, the Average function is called to begin calculation. Inside the Average function, the user is prompted for an integer value repeatedly up until the upper bound. Using the sum of these values, the program calculates the average. If the user inputs a non integer value, the program will alert the user that the value must be an integer and ask again. If the user again inputs a non integer value, that iteration will be omitted from the final average. The program this prints the calculated average to the user.
Cheers.