In: Computer Science
In Python, write a program to get a positive integer n from keyboard first (your program must be able to check the validation of n being positive), and then get n integers from keyboard and store these n integers in a list. Do some processing, and print out average of the elements in the list at end of your program.
For example, suppose user enters 3 first (n = 3), which means we get another three integers from the user. Suppose these three integers are 1, 2, -6. Store them in a list. It is easy to see the list is [1, 2, -6], and average of the elements in the list is (1 + 2 - 6) / 3 = -1.
Below is the python code for mentioned problem statement:
# Program will start from here
if __name__ == '__main__':
# User input
count = int(input("How many numbers you want to enter : "))
# Empty List
list_num = []
if count >0:
# for loop to take user number for num
for x in range(count):
num = int(input("Enter the number : "))
list_num.append(num)
print("Average of elements : ", int(sum(list_num)/len(list_num)))
else:
print("Enter positive number. Try Again!")
