I am starting to code in python. When I was to take two inputs from user with a space between the two inputs my code was like
min, p = input().split(" ")
min=int(min)
p=float(p)
which worked fine. In another such problem I am to take a n*n matrix as user input which I declared as arr=[[0 for i in range(n)] for j in range(n)]
printing the arr gives a fine matrix(in a single row though) but I am to replace each element '0'with a user input so I use nested loops as
for i in range(0,n)
for j in range(0,n)
arr[i][j]=input()
this also worked fine but with a press of 'enter' button after each element. In this particular problem the user will input elements in one row at space instead of pressing 'enter' button. I wanted to know how to use split in this case like in first case above, keeping in mind the matrix is n*n where we don't know what is n. I prefer to avoid using numpy being a beginner with python.
Try with below,
Well if matrix is n*n that means with first input line you know number of input lines (and no, it's impossible for
input()
to not end with key enter is pressed). So you need something like this:I used
range(len(arr[0]) - 1)
so it inputs rest of lines (because matrix width and height is same and one first line is already read from input).Also I used
.split()
without" "
as parameter because it's default parameter.You can try this simple approach (press enter after each digit...works fine)::
You can do this:
Now you input in the console values like this: