How to create a board of user-determined size and present it as text using python?

Asked

Viewed 1,885 times

0

I am trying to create a program in Python 3.6.1 that creates a board according to the size typed by the end user and prints the result in the shell.

It needs to be presented as text, and cannot use any library. As much as the library is internal.

Example: if the user enters a board with 10 rows and 15 columns. The program would have to generate this board 10x15. Since all its values must be cells filled by strings. Because, later, the user will enter the value "P", for example, it will change a cell predefined by me. Then the board would be printed again with the value of the previously defined cell by me.

EX: User enters initial information:

Insert number of lines: 10

Enter the number of columns: 15

the board would initially look like this:

  0123456789.....
0 XXXXXXXXXXXXXXX
1 XXXXXXXXXXXXXXX
2 XXXXXXXXXXXXXXX
3 XXXXXXXXXXXXXXX
4 XXXXXXXXXXXXXXX
5 XXXXXXXXXXXXXXX
6 XXXXXXXXXXXXXXX
7 XXXXXXXXXXXXXXX
8 XXXXXXXXXXXXXXX
9 XXXXXXXXXXXXXXX

Then the user would insert P in the 3x3 coordinates, for example:

enter what you want in row 3 column 3: P <<< user typed P

After that, the board would be printed again, with the following result:

 

   0123456789.....
 0 XXXXXXXXXXXXXXX
 1 XXXXXXXXXXXXXXX
 2 XXXXXXXXXXXXXXX
 3 XXXPXXXXXXXXXXX #o P seria inserido nessa linha. Exatamente onde foi mandado.
 4 XXXXXXXXXXXXXXX
 5 XXXXXXXXXXXXXXX
 6 XXXXXXXXXXXXXXX
 7 XXXXXXXXXXXXXXX
 8 XXXXXXXXXXXXXXX
 9 XXXXXXXXXXXXXXX

Can anyone tell me how to do that?

The user questions part, I know how to do.

My problem is with the board. I have no idea how to leave as I said. Would creating a matrix be a good solution? Or is there a less complicated way? I have tried several ways to generate this board, but without success.

I do not know if I am wrong in logic or syntax, because I started programming with python recently.

I did a lot of research on a possible solution, but found no clear information.

I have more experience with php currently.

All help is welcome.

From now on, thank you.

PS: The closest result I could get to what I needed was this:

linhas = int(input('insira a quantidade de linhas:'))
colunas = int(input('insira a quantidade de colunas:'))
celula = "X"

for a in range(linhas):
    print(celula*colunas)

He generates everything apparently as needed. But that’s not quite how the end result should look. I need each X printed on the screen to be a dynamic "space", where each can be changed later with any other value according to the coordinates. Ex: F in row 4 column 7 . I don’t know how to do this.

2 answers

1

What I did was something more complex, and with some unnecessary things, but it gets the same result.

Because of the spaces, the board can only have 56 columns (I think you can increase this amount if the monitor is bigger, otherwise everything gets bugged).

I put spaces by that after arriving in the 10th column and forward there is no way to put "10" on top of a single "X"

linhas = int(input('Quantas linhas? : '))
colunas = int(input('Quantas colunas? : '))

print('\n    |  ',end='')
for i in range(0,colunas):
        if i > 9:
                print(str(i)+' ', end='') #Retirei um espaço para que a posição dos numeros não fique errada
        else:
                print(str(i)+'  ', end='')
#print('|','\n   |'+'-'*3+'-'*((colunas*3)-1)+'|') #Um traço que entre os numeros da coluna
print('|')
for i in range(0,linhas):
        if i >= 10 and i < 100:
                print(i,' |'+'  X'*colunas,' |') #Linhas maiores que 9 e menores que 100 (10-99)
        elif i >= 100:
                print(i,'|'+'  X'*colunas,' |') #Linhas maiores que 9 e menores que 100 (10-99)
        else:
                print(i,'  |'+'  X'*colunas,' |') #Linhas menores que 10

while 1:
        local_c = int(input('\nQual local da coluna? : '))
        if local_c > colunas-1:
                print('local inválido.\n')
                continue
        break

while 1:
        local_l = int(input('Qual local da linha? : '))
        if local_l > linhas-1:
                print('local inválido.\n')
                continue
        break

print('\n    |  ',end='')
for i in range(0,colunas): #Numeros da coluna
        if i > 9:
                print(str(i)+' ', end='') #end='' serve para impedir a quebra de linha "\n"
        else:
                print(str(i)+'  ', end='')
print('|')
for i in range(0,linhas):
        if i == local_l: #Linha que será alterada
                c1 = local_c
                c2 = (colunas-c1)-1
                if i > 9 and i < 100:
                        print(str(i),' |'+'  X'*c1,' P'+'  X'*c2,' |') #Linhas maiores que 9 e menores que 100 (10-99)
                elif i >= 100:
                        print(str(i),'|'+'  X'*c1,' P'+'  X'*c2,' |') #Linhas maiores que 9 e menores que 100 (10-99)
                else:
                        print(i,'  |'+'  X'*c1,' P'+'  X'*c2,' |') #Linhas menores que 10
        elif i >= 10 and i < 100:
                print(i,' |'+'  X'*colunas,' |') #Linhas maiores que 9 e menores que 100 (10-99)
        elif i >= 100:
                print(i,'|'+'  X'*colunas,' |') #Linhas maiores que 99
        else:
                print(str(i),'  |'+'  X'*colunas,' |') #Linhas menores que 10

exit:

>>> Quantas linhas? : 10
>>> Quantas colunas? : 10
>>> 
>>>     |  0  1  2  3  4  5  6  7  8  9  |
>>> 0   |  X  X  X  X  X  X  X  X  X  X  |
>>> 1   |  X  X  X  X  X  X  X  X  X  X  |
>>> 2   |  X  X  X  X  X  X  X  X  X  X  |
>>> 3   |  X  X  X  X  X  X  X  X  X  X  |
>>> 4   |  X  X  X  X  X  X  X  X  X  X  |
>>> 5   |  X  X  X  X  X  X  X  X  X  X  |
>>> 6   |  X  X  X  X  X  X  X  X  X  X  |
>>> 7   |  X  X  X  X  X  X  X  X  X  X  |
>>> 8   |  X  X  X  X  X  X  X  X  X  X  |
>>> 9   |  X  X  X  X  X  X  X  X  X  X  |
>>> 
>>> Qual local da coluna? : 5
>>> Qual local da linha? : 5
>>> 
>>>     |  0  1  2  3  4  5  6  7  8  9  |
>>> 0   |  X  X  X  X  X  X  X  X  X  X  |
>>> 1   |  X  X  X  X  X  X  X  X  X  X  |
>>> 2   |  X  X  X  X  X  X  X  X  X  X  |
>>> 3   |  X  X  X  X  X  X  X  X  X  X  |
>>> 4   |  X  X  X  X  X  X  X  X  X  X  |
>>> 5   |  X  X  X  X  X  P  X  X  X  X  |
>>> 6   |  X  X  X  X  X  X  X  X  X  X  |
>>> 7   |  X  X  X  X  X  X  X  X  X  X  |
>>> 8   |  X  X  X  X  X  X  X  X  X  X  |
>>> 9   |  X  X  X  X  X  X  X  X  X  X  |

I hope I helped you, if you want, I can simplify the code.

0


See if it helps you, I commented on the code to give you a sense of what’s going on.

My suggestion is that you run part by part to understand the functioning

Code:

#pergunta a qtd de linhas e colunas para o usuario
lins = int(input("Insira o num de linhas: "))
cols = int(input("Insira o num de colunas: "))

#cria uma lista com o "X" aparecendo tantas vezes quando o nºde colunas
table = []
for i in range(lins):
    table.append(list('X'*cols))

#une as listas sem nenhum espaco para mostrar na tela
for line in table:
    print(''.join(line))

#pergunta qual linha e coluna deseja alterar
lin_alterar = int(input("Qual linha deseja alterar?: "))
col_alterar = int(input("Qual coluna deseja alterar?: "))

#qual o valor novo a ser inserido
vlr_novo = 'P'

#altera o item da posicao escolhida para o vlr_novo
table[lin_alterar - 1][col_alterar - 1] = vlr_novo

#une as listas sem nenhum espaco para mostrar na tela
for line in table:
    print(''.join(line))

Output:

>>> Insira o num de linhas: 5
>>> Insira o num de colunas: 10
>>> XXXXXXXXXX
>>> XXXXXXXXXX
>>> XXXXXXXXXX
>>> XXXXXXXXXX
>>> XXXXXXXXXX
>>> Qual linha deseja alterar?: 3
>>> Qual coluna deseja alterar?: 2
>>> XXXXXXXXXX
>>> XXXXXXXXXX
>>> XPXXXXXXXX
>>> XXXXXXXXXX
>>> XXXXXXXXXX

In essence, your default table in this example has the following format:

[['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X']]

It is a list containing M lists with N items, where:

  • M = no lines
  • N = no columns

Browser other questions tagged

You are not signed in. Login or sign up in order to post.