Posted on:

14 Jul 2022

0

# Collect input from the user and store in a numpy array ( Q5,Section 6,Python bootcamp)

Question 5
Ask the user to input a sequence of numbers. Then calculate the mean
and print the result.
Here is the solution provided in the course.
Nice and clean, works perfectly fine.

``````user_input = input('Please enter a number type exit to stop:> ')
numbers = []
while user_input.lower() != 'exit':
while not user_input.isdigit():
print('That is not a number! Numbers only please:> ')
user_input = input('Try again:> ')
numbers.append(int(user_input))
user_input = input('Please enter next number:> ')
total = 0
for number in numbers:
total += number

print(f'Mean is {total/len(numbers)}')
print(sum(numbers)/len(numbers))
``````

Now, my goal is to attempt the question using the following logic. and with slight modification to the above code.
collect the numbers from the user
store them one by one in a NumPy array
use the np.ndarray.mean to get the mean.

``````# modification to above solution
import numpy as np
user_nums = np.array([])
user_input = input('Please enter a number type exit to stop:> ')
while user_input.lower() != 'exit':
while not user_input.isdigit():
print('That is not a number! Numbers only please:> ')
user_input = input('Try again:> ')
np.append(user_nums,float(user_input))  # append user input to user_nums

mean = user_nums.mean()
print("mean is ",mean)

``````

After inputting the first number, the program takes forever to continue running leaving me no choice but to force-close the program. Can you identify my mistakes? Thanks for your kind assistance in advance.