Hello! I am stuck with the calculator part. I have followed every way I have seen to do the mean however it comes up with error and I cannot work out why;

Traceback (most recent call last):
File “C:\Users\lydia\PycharmProjects\Mean Calculator\Mean Calculator.py”, line 16, in
mean_row = np.mean(a, axis = 0).tolist()
File “<array_function internals>”, line 5, in mean
File “C:\Users\lydia\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\core\fromnumeric.py”, line 3419, in mean
return _methods._mean(a, axis=axis, dtype=dtype,
File “C:\Users\lydia\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\core_methods.py”, line 178, in _mean
ret = umr_sum(arr, axis, dtype, out, keepdims, where=where)
TypeError: cannot perform reduce with flexible type

Has anyone else found this? The mean section is my latest attempt, I swear I’ve tried every tutorial I’ve seen. This is all of my code so far;

import numpy as np
n = ()
while len(n) != 9:
n = list(input("Enter 9 numbers (no spaces): "))
if len(n) != 9:
print("Must be 9 numbers")
else:
break;
n_array = np.array(n)
print(n_array)
a = np.array(n).reshape(3, 3)
print(a)
mean_row = np.mean(a, axis = 0).tolist()
print(mean_row)

P.S not sure why the indents have vanished when I posted on here, but they are there in real life

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

Quick tipp: You can literally copy the error message into google and the first result would be the solution:
Your input is treated as “strings”, hence the mean-calculation just fails. So all you gotta do is define the type.

n_array = np.array(n).astype(np.int)
Also your reshape is not referring to n_array.

And as a little bonus: Please make a better input. Took me like 3 minutes to even figure out a valid input and it’s 123456789. Soooo, that’s not how anybody would input numbers to calculate a mean.

Thank you! Of course! I initially had int incorporated to the input but I forgot I removed it. I think once everything works I’ll change the phrasing, maybe “Please enter 9 numbers to calculate the mean… etc” something like that anyway haha I’m quite dyslexic so who knows how the input will turn out haha