How to create a user-sized tray and display it as text using python?

0

I'm trying to create a program in Python 3.6.1 that creates a tray according to the size entered by the end user and prints the result in the shell.

It needs to be displayed as text, and you can not use any library. As much as the library is internal.

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

EX: The user enters the initial information:

enter the number of rows: 10

enter the number of columns: 15

the board would 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 into 3x3 coordinates, for example:

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

After this, 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

Does anyone know how to do this?

The questions part to the user, I know how to do.

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

I do not know if I'm wrong in logic or syntax, since I started programming with python recently.

I researched a lot about a possible solution but found no clear information.

I have more experience with php currently.

All help is welcome.

Thanks in advance.

PS: The closest result I needed to do 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)

It generates everything apparently as accurate. But that's not the way the final result should be. I need each X printed on the screen to be a dynamic "space", where each one can be changed later with any other value according to the coordinates. Ex: F in row 4 column 7. I do not know how to do that.

    
asked by anonymous 14.04.2017 / 09:01

2 answers

0

See if this helps, I commented the code to give you an idea of what is happening.

My suggestion is to go from side to side to understand the operation

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 = number of columns
14.04.2017 / 22:18
1

What I did was something more complex, and with some unnecessary things, but that got 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 larger, otherwise everything is bugged).

I put spaces because after arriving in the 10th column and then there is no such as "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

Output:

>>> 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 have helped you, if you want, I can simplify the code.

    
14.04.2017 / 23:20