0
print("Hi sir!")
X=list(input("please enter the first matrix X="))
Y=list(input("Please enter the second matrix Y="))
result=list(input("please enter the template of the result matrix in the form of zeros (0)="))
# iterate through rows of X
for i in range(len(X)):
   # iterate through columns of Y
   for j in range(len(Y[0])):
       # iterate through rows of Y
       for k in range(len(Y)):
           result[i][j] += X[i][k] * Y[k][j]

for r in result:
   print(r)

With this code, I get an error that says "can't multiply sequence by non-int of type 'str'".

in this I need the user himself to put the matrices he want however when I put the same matrices in the code editor it works so I think the errors comes from the way of defining the input...

Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67

1 Answers1

0

You neglected to convert the input to a numeric type. Python input comes in string form. Rather than

X=list(input("please enter the first matrix X="))

you'll need to extract the individual elements to something numeric. That said ... shame on you for not bothering to print the values causing the problem. You wrote a triple-nested loop without checking the values you sent into it. A simple

print X

Would show the basic problem. This is a low-tech, but powerful debugging tool: if you have a sick patient, ask where it hurts; prod a few places. Then you won't have to come to the emergency room (StackOverflow). :-)

That said, let's get back to the input processing. I expect that you'll want to take the matrix, one line at a time, grab the individual numbers, and convert them to floats. Something like this ...

in_line = input("please enter the first matrix X=")
value_list = in_line.split()
X = [float(x) for x in value_list]

You can reduce this to a single line, but this helps show you the stages. Repeat this for each line in the matrix, and then make a list of those lists. Do likewise for Y.

Does that get you unstuck?

Prune
  • 76,765
  • 14
  • 60
  • 81
  • I didn't get your last point could you please give me an example ? – Mahmoud Bassuoni Nov 09 '15 at 20:19
  • Not quite, for two reasons: (1) StackOverflow is for specific coding problems, not a tutorial service. I sense that you have a conceptual problem with the data structures you're trying to handle; (2) I don't know what you imagine for input and your resulting data structure. You *really* need to finish your posting per [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). We've explain the problem you posted; you should now work with what we've given you. If you have another problem, please post another question. Otherwise, we get into a long tutorial session. – Prune Nov 09 '15 at 21:01