0

I am making a Sudoku solver in Python, and my code works, but I need to specify a list consisting of 9 lists within a list for it to be analyzed. I am currently struggling on how to make it so that when the user is prompted for an input, they enter in 9 numbers without spaces, then do it again 8 times; each time they submit a row, the row gets converted into a list. Currently I have to manually specify the list (see example below).

b=[[2,5,9,7,1,6,4,8,3],
[8,6,7,3,4,5,9,1,2],
[4,1,3,9,2,8,6,7,5],
[3,9,8,5,7,4,1,2,6],
[5,4,6,2,8,1,7,3,9],
[1,7,2,6,3,9,5,4,8],
[9,8,4,1,6,3,2,5,7],
[6,2,1,8,5,7,3,9,4],
[7,3,5,4,9,2,8,6,1]]

I was thinking of doing a while loop with a split function and then appending each value into a list, but that just creates one giant list.

Can someone help me?

I can expand on this or give you code if necessary, thanks.

3 Answers 3

2

Input is separated without spaces

b = [[int(x) for x in input()] for i in range(9)]

Wouldn't it look nicer if it was seperated by spaces, I know your question specifies not to but you could do this:

b = [[int(x) for x in input().split()] for i in range(9)]

note: raw_input on Python 2.7

Sign up to request clarification or add additional context in comments.

Comments

0

First, find out what the len() of their first input is, then create a list of that length. Then, each time though your loop, create a list and add it to your container list. Stop asking for input once you've got the right number of lists to build your rows. There are several ways of accomplishing this with either while or for loops.

Comments

0

You could use a list comprehension on the input and then append that to your main list:

main = []
...
#get input
...
main.append([int(i) for i in input])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.