Home > database >  How to add numbers for one to 8 on the left and righ side of this function
How to add numbers for one to 8 on the left and righ side of this function

Time:11-25

Hi I have problem i dont knbwo how to add numbers order 1 to 8 on the left side and right side of this function. And another problem is there is always showing none when I print it I dont know why I thouth it was beacouse my function was empty but that din't help . So what can I do with this thank you very much. Here is picture how it should look like

sachy = [[0, 1, 0, 1, 0, 1, 0, 1],[1, 0, 1, 0, 1, 0, 1, 0],[0, 1, 0, 1, 0, 1, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0],[0, 2, 0, 2, 0, 2, 0, 2],[2, 0, 2, 0, 2, 0, 2, 0],[0, 2, 0, 2, 0, 2, 0,2]]
poradi =["a", "b", "c", "d", "e", "f", "g", "h"]
poradi_2= [1,2,3,4,5,6,7,8]

 



def sachovnice(nula,jedna,dva):


             
     
    for radek in sachy: 
        for i in radek:
            if i == nula:
                print('.', end = " ")
            elif i == jedna:
                print('o', end = " ")
            elif i == dva:
                print('*', end = " ")
            else:
                print(i, end = " ")
        
        print( )
    print( " ".join(poradi))    
print( " ".join(poradi))       
    


print(sachovnice(0,1,2))

I try to add for b´t othe def but taht will print everything 8 times even the lists.Dont know about that none thats still showing up.

CodePudding user response:

Ok, I shortened a bit your code. Mainly replaced multiple if conditions with a replacement map, that you can pass to your function.

sachy = [[0, 1, 0, 1, 0, 1, 0, 1],[1, 0, 1, 0, 1, 0, 1, 0],[0, 1, 0, 1, 0, 1, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0],[0, 2, 0, 2, 0, 2, 0, 2],[2, 0, 2, 0, 2, 0, 2, 0],[0, 2, 0, 2, 0, 2, 0,2]]
poradi =["_", "a", "b", "c", "d", "e", "f", "g", "h", "_"]

repl_map = {
    0: ".",
    1: "o",
    2: "*"
}

def sachovnice(repl_map):
    print(" ".join(poradi))  
     
    for i, radek in enumerate(sachy): 
        print(f"{i 1} "   " ".join([repl_map.get(cell) for cell in radek])   f" {i 1}")
      
    print( " ".join(poradi))       
    
sachovnice(repl_map)

CodePudding user response:

Without changing your own code too much, please see below a fix to it. Igor's approach is a better way to do it, if you can understand what it does.

sachy = [[0, 1, 0, 1, 0, 1, 0, 1],[1, 0, 1, 0, 1, 0, 1, 0],[0, 1, 0, 1, 0, 1, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0],[0, 2, 0, 2, 0, 2, 0, 2],[2, 0, 2, 0, 2, 0, 2, 0],[0, 2, 0, 2, 0, 2, 0,2]]
poradi = [" ", "a", "b", "c", "d", "e", "f", "g", "h", " "]

def sachovnice(nula, jedna, dva):
    x = 1
    result = " ".join(poradi)   '\n'

    for radek in sachy:
        result  = f"{x} "
        for i in radek:
            if i == nula:
                result  = '. '
            elif i == jedna:
                result  = 'o '
            elif i == dva:
                result  = '* '
        result  = f"{x}\n"
        x  = 1
    
    result  = " ".join(poradi)

    return result


print(sachovnice(0,1,2))
  • Related