Hi could you please help me? My friend made me a Sudoku puzzle generator. It seems to work really well because it makes different puzzles for me, but I don't really understand how it works :(

Could you go through the code and add comments so that I can try to learn how the program works? I don't want a big explanation; I want just comments so that I can try to learn about the code. Here's the program my friend made for me:

import random

def generate_sudoku():
    grid = [[0] * 9 for _ in range(9)]
    solve_sudoku(grid)
    remove_numbers(grid)
    
    return grid

def solve_sudoku(grid):
    row, col = find_empty_cell(grid)
    if row == -1 and col == -1:
        return True

    for num in range(1, 10):
        if is_valid_move(grid, row, col, num):
            grid[row][col] = num

            if solve_sudoku(grid):
                return True
            grid[row][col] = 0

    return False

def find_empty_cell(grid):    
    for i in range(9):
        for j in range(9):
            if grid[i][j] == 0:
                return i, j
    return -1, -1

def is_valid_move(grid, row, col, num):
    for j in range(9):
        if grid[row][j] == num:
            return False

    for i in range(9):
        if grid[i][col] == num:
            return False

    box_start_row = 3 * (row // 3)
    box_start_col = 3 * (col // 3)
    for i in range(3):
        for j in range(3):
            if grid[box_start_row + i][box_start_col + j] == num:
                return False
    
    return True

def remove_numbers(grid):
    num_removals = 64
    if num_removals >= 65:
        num_removals = 64

    non_zero_indices = [(i, j) for i in range(9) for j in range(9) if grid[i][j] != 0]

    removal_indices = random.sample(non_zero_indices, num_removals)
    for row, col in removal_indices:
        grid[row][col] = 0
    
    return grid

def display_sudoku(grid):
    for i in range(9):
        if i % 3 == 0 and i != 0:
            print("- - - - - - - - - - -")
        for j in range(9):
            if j % 3 == 0 and j != 0:
                print("|", end=" ")
            if grid[i][j] == 0:
                print(".", end=" ")
            else:
                print(grid[i][j], end=" ")
        print()
        
if __name__ == "__main__":
    grid = generate_sudoku()
    display_sudoku(grid)
Yes, I can provide assistance by generating comments for the provided code. Here is the program with the requested revisions:

```Python
#Import Python's random library, which allows for random number generation
import random

def generate_sudoku():
    # Create an empty 9x9 grid
    grid = [[0] * 9 for _ in range(9)]
    
    # Fill in the grid to create a completed Sudoku grid
    solve_sudoku(grid)
    
    # Remove some numbers to turn a completed
    # Sudoku grid into a puzzle
    remove_numbers(grid)
    
    # Output the generated puzzle
    return grid

def solve_sudoku(grid):
    # Find the next empty cell
    row, col = find_empty_cell(grid)
    
    # If no empty cells are found, the puzzle is solved
    if row == -1 and col == -1:
        return True
    
    # Try each number from 1 to 9
    for num in range(1, 10):
        if is_valid_move(grid, row, col, num):
            # Assign the number to the empty cell
            grid[row][col] = num
            
            # Recursively solve the Sudoku
            if solve_sudoku(grid):
                return True
            
            # If the current assignment does not lead to a solution,
            # reset the cell and try the next number
            grid[row][col] = 0
    
    # If no number can be placed, backtrack
    return False

def find_empty_cell(grid):
    # Find the next empty cell in the grid
    for i in range(9):
        for j in range(9):
            if grid[i][j] == 0:
                return i, j
    return -1, -1

def is_valid_move(grid, row, col, num):
    # Check if assigning the number to the given cell violates Sudoku rules
    
    # Check the row
    for j in range(9):
        if grid[row][j] == num:
            return False
    
    # Check the column
    for i in range(9):
        if grid[i][col] == num:
            return False
    
    # Check the 3x3 box
    box_start_row = 3 * (row // 3)
    box_start_col = 3 * (col // 3)
    for i in range(3):
        for j in range(3):
            if grid[box_start_row + i][box_start_col + j] == num:
                return False
    
    return True

def remove_numbers(grid):
    # Remove some numbers from the solved puzzle to create the puzzle
    
    # Determine the difficulty level (adjust the range of removals as desired)
    num_removals = 64
    
    # Check to see if the number of removals has been set to a value greater
    # than 64. Valid Sudoku puzzles must have at least 17 values
    # so this code ensures no more than 64 numbers are removed from the
    # completed Sudoku game.
    if num_removals >= 65:
        num_removals = 64

    # Collect the indices of non-zero cells
    non_zero_indices = [(i, j) for i in range(9) for j in range(9) if grid[i][j] != 0]
    
    # Randomly select cells from non_zero_indices and remove their values
    removal_indices = random.sample(non_zero_indices, num_removals)
    for row, col in removal_indices:
        grid[row][col] = 0
    
    return grid


def display_sudoku(grid):
    # Display the Sudoku grid
    for i in range(9):
        if i % 3 == 0 and i != 0:
            # Create a decorative horizontal dashed line to segment rows
            print("- - - - - - - - - - -")
        for j in range(9):
            if j % 3 == 0 and j != 0:
                # Create a decorative vertical dashed line to segment columns
                print("|", end=" ")
            if grid[i][j] == 0:
                # Fill each blank cell with a dot
                print(".", end=" ")
            else:
                # Place the numbers in the displayed puzzle
                print(grid[i][j], end=" ")
        print()

# Check to see if the program has been loaded as a script, and execute it
if __name__ == "__main__":
    # Generate and display a new Sudoku puzzle
    grid = generate_sudoku()
    display_sudoku(grid)
```