Search code examples
pythonsudoku

How to import a sudoku board in txt in Python?


in one of my master degree classes I have to create in python a sudoku game (with special rules). My professor request is that I need to import a .txt file with the sudoku board (for me created) to my code file.

I do like this: **

def import_boardsudoku(filename):
    file=open(filename,'r')
    content=file.read()
    file.close()
    return content

    hs = []
    for line in content:
        hs_line = line.replace("\n"," ").split(",")
        hs.append(hs_line)

    for i in range(len(hs)):
        for j in range(len(hs[i])):
            if hs[i][j] == "0":
                hs[i][j] = " "
            else:
                hs[i][j]=int(hs[i][j])

    return hs
print import_boardsudoku("sudoku_hs.txt")

**

But I don´t understand:

  1. Why my zeros don´t are replaced by whitespace
  2. Why I can´t use my hs (boardsudoku imported) in the rest of my code (like print boardgame or validation of the rules, etc)

Inside my .txt file its: inside my .txt file its:

Thank you very much for your help! :)


Solution

  • everything below

    return content
    

    is unusable as you are returning early.