Sudoku with user input

2024/11/18 18:22:12

I am trying to write a Sudoku game with user input. So the user can choose what row/column it wants to and what number. I have to import it from a text file and have made a save and load function.

I have tried starting with what I know, but I don't know how to proceed, ex. how to make the board correspond with the user input.

sudoku_brett.txt

    0 1 2   3 4 5   6 7 8 +-------+-------+-------+
0 | 0 0 6 | 9 0 5 | 0 1 0 |
1 | 9 7 0 | 0 1 2 | 3 0 5 |
2 | 0 2 0 | 0 0 4 | 8 6 0 |+-------+-------+-------+
3 | 5 0 3 | 8 0 0 | 0 2 0 |
4 | 0 0 0 | 0 0 0 | 0 0 0 |
5 | 0 8 0 | 0 0 1 | 9 0 7 |+-------+-------+-------+
6 | 0 5 4 | 1 0 0 | 0 7 0 |
7 | 2 0 7 | 4 5 0 | 0 9 3 |
8 | 0 6 0 | 7 0 3 | 1 0 0 |+-------+-------+-------+

main code

import save_load
import info
import sudoku_brettprint("Velkommen til sudoku, dette er brettet ditt" readFromFile('sudoku_brett.txt')) #giving them an importet boardprint("Vil du gjøre endringer på brettet?") #asking what changes the user wants to makecol = int(input('Hvilken kolonne vil du endre? ')) #changing column
row = int(input('Hvilken rad vil du endre? ')) #changing row
nytt_tall = int(input('Hva skal det nye tallet være? ')) #asking what number the user would likebrett[row - 1][col - 1] = nytt_tallprint('Ditt nye brett:\n') #printing out the board, with the new input numberbruker.print_board(sudoku_brett)

info.py

def check_input(user_input):if user_input.isdigit():if 1 <= int(user_input) <= 9:return Trueprint ("Det kan kun være tall mellom 1-9. Prøv igjen") #telling it can only be number from 1-9return Falsedef print_board(board):for row in board:print(row)def generate_empty_board():return [[[[0 for i in range(3)] for i in range(3)] for i in range(3)] for i in range(3)]print_board(generate_empty_board())

save_load.py

 def readFromFile(filename):f = open(filename,'r')innhold = f.read()print(innhold)f.close()def save(filename, board):f = open(filename, 'sudoku_brett')pickle.dump(board, f)f.close()print("Brett lagret") #saveddef load(filename):f = open(filename, 'sudoku_brett')board = sudoku_brett(f)f.close()print("Brett lastet") #loadedreturn board
Answer

The only way it would work in the terminal from what I know is to wait for user input, and they then enter the row, column and number into the terminal. Here is an example:

row = input("Enter a row: ")
col = input("Enter a column: ")
num = input("Enter a number to insert into the grid: ")

And then you would use the values entered to draw the updated grid which you would print to the terminal again, and then await user input again.

https://en.xdnf.cn/q/120043.html

Related Q&A

Move user to a textchannel as soon as he has pressed a button

I have a problem. I would like to move a user from one specific textchannel to another specific textchannel as soon as he has pressed a button. Unfortunately I get an error. class MyView(discord.ui.Vie…

scrape the about page of websites with Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Tkinter entry widget execution [duplicate]

This question already exists:Entry widget in tkinterClosed 2 years ago.So I made a simple program however it doesnt seem to work my code is: e = Entry(root, font = 20,borderwidth=5)e.grid(row=1)def cap…

Python Redefine the variable given as a parameter to the function

Hello I am trying to make theme window with tkinter. There is 5 variable for different widgetss color. I will use color dialog for choosing colors but I dont want to define 5 functions. So I think I ca…

Need a Gui Keypad for a touchscreen that outputs a pin when code is correct

I have a raspberry pi with a touchscreen running raspbian, Im hoping to have a Gui on the touchscreen that had a number keypad that when a correct input is entered a pin will output to a door latch or …

How can i make the python to wait till i complete speaking?

I am writing a program to recognise the speech from a microphone and the code will process accordingly. The code I wrote for this purpose is below.import speech_recognition as sr import webbrowser impo…

Facing AttributeError: list object has no attribute lower

I have posted my sample train data as well as test data along with my code. Im trying to use Naive Bayes algorithm to train the model.But, in the reviews Im getting list of list. So, I think my code is…

why I cannot use max() function in this case? [duplicate]

This question already has answers here:Why do I get "TypeError: int object is not iterable" when trying to sum digits of a number? [duplicate](4 answers)Closed 1 year ago.n,m,k=map(int, inpu…

SQLALchemy and Python - Getting the SQL result

I am using cloudkitty which is rating module in OpenStacks.But here question is regarding the SQLAlchemy and Python.I am new to SQLAlchemy.I need to fetch some details from a table using a API call.So …

ValueError: invalid literal for int() with base 10: python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Closed 10 years ago.Questions asking for code must demonstrate a minimal understanding of the proble…