Understanding the Rewrite of Elements in a List Using the for and if statements

Asked 2 years ago, Updated 2 years ago, 61 views

I'm a beginner, so I'm sorry if I don't know how to ask questions.

I'm trying to create a matrix in Python 3 and write a code that replaces the elements in the matrix with input results.

It was possible to create a matrix with any number of elements in the basic_matrix in the figure, but how can I write a code to input all elements in the matrix and reflect the value in the basic_matrix?
For the time being, I tried to return the document according to the input value as shown in the image, but it didn't work either.

Enter a description of the image here

python numpy

2022-09-30 18:38

1 Answers

Input receives a string (str), so it is all NG compared to an integer (int).

if int(input("if relevant…")==1:

If you convert it to an integer like this, it will respond properly to the input value.

If you want to compare with the contents of the array (basic_matrix),

print(basic_matrix==1)

If you write the above outside of the for statement, it will remain in the form of an array, return the matching one with True, and return the non-conforming one with False.↓

 [[True False False False]
 False True False False False
 False False True False False
 False False False True False
 False False False False True

I hope it will be helpful.


2022-09-30 18:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.