How to use files in python

-2

I'm still finishing a program as a prog I work and I'm having a lot of trouble with the files part. I am not able to insert an array into the file. Does anyone know how to do this?

The program is about Einstein's game, and has to have the following requirements in the files section:

  

Option 3: Print a report with a history of   played (words and filled positions), in addition to the updated matrix and   of the score (number of hits), for each player registered.

     

The Archive should also contain the average number of plays between players to achieve success (complete the matrix), as well as which player completed the matrix with the least number of moves and the one with the most moves.

/ p>      

Upon completion of the report, inform the user of the name of the generated text file.

The program for now looks like this: (delete what is not needed for files, like the result array)

arqParcial=open("parcial.txt","w")
arqParcial.write("Relatório de Jogadas: \n")
arqParcial.write("Coluna / Alteração\n")
arqParcial.close()
matriz=[]
for i in range (6):
    linha=['-']*6
    matriz.append(linha)
matriz[0][0]=':D'
matriz[0][1]='Casa 1'
matriz[0][2]='Casa 2'
matriz[0][3]='Casa 3'
matriz[0][4]='Casa 4'
matriz[0][5]='Casa 5'
matriz[1][0]= '1.Cor'
matriz[2][0]= '2.Nacionalidade'
matriz[3][0]= '3.Bebida'
matriz[4][0]= '4.Cigarro'
matriz[5][0]= '5.Animal'
for i in range(6):
    print(matriz[i])

print('') 
print('Dicas: \nO Norueguês vive na primeira casa. \nO Inglês vive na casa Vermelha. \nO Sueco tem Cachorros como animais de estimação. \nO Dinamarquês bebe Chá. \nA casa Verde fica do lado esquerdo da casa Branca. \nO homem que vive na casa Verde bebe Café. \nO homem que fuma Pall Mall cria Pássaros. \nO homem que vive na casa Amarela fuma Dunhill. \nO homem que vive na casa do meio bebe Leite. \nO homem que fuma Blends vive ao lado do que tem Gatos. \nO homem que cria Cavalos vive ao lado do que fuma Dunhill. \nO homem que fuma BlueMaster bebe Cerveja. \nO Alemão fuma Prince. \nO Norueguês vive ao lado da casa Azul. \nO homem que fuma Blends é vizinho do que bebe Água.')
print('')
print('OBS: Digite tudo como indicado acima, incluindo os acentos.')
print('')
linha=input('Digite o número correspondente a linha que deseja alterar(1.Cor; 2.Nacionalidade; 3.Bebida; 4.Cigarro; 5.Aninal) ou Desisto ou Acabei: ')
linha=linha.upper()


while linha != 'ACABEI' and linha != 'DESISTO':
    linha=int(linha)
    coluna=int(input('Digite o número correspondente a coluna que deseja alterar (Casa 1; Casa 2; Casa 3; Casa 4; Casa 5: '))
    novo=input('Digite a alteração: ')
    novo=novo.lower()
    matriz[linha][coluna]=novo
    visu=input('Deseja visualizar a tabela (S ou N)? ')
    visu=visu.upper()
    with open("parcial.txt", "a") as arqParcial:
        arqParcial.writelines(str(coluna)+ "  /  ")
        arqParcial.writelines(str(novo)+"\n")
        for i in range(6):
            arqParcial.writelines(matriz[i])
    if visu == 'S':
        for i in range(6):
            print(matriz[i])

But in this part:

for i in range(6):
            arqParcial.writelines(matriz[i])

It gives error. Can someone help me?

    
asked by anonymous 09.03.2016 / 00:50

1 answer

2

At first glance, there is no problem in the program that would justify an error on the line you indicate - but there are several other problems in building the program, which will make it not work as planned, and can lead to errors in that program. or elsewhere.

Already uqe you had a problem with the method writelines start there: this method is used when you want to pass an iterable - for example a list of strings - to be saved consecutively in the file. Coincidentally it works when you pass a single string too - why Python considers a string to be an interleave of strings of a single letter - that is, when you write: arqParcial.writelines(str(coluna)+ " / ") Python resolves the expression in parentheses, which results in a string of type "1 /" - and writelines writes in the file the characters "1", "", "/" and "", as if each were a line. In these cases, the best way is to use the write method of the file, not writelines .

In the line that you indicate, the use of writelines would be correct - but will only work if each element of the list passed to the file is a string. If any element is different from a string (such as a number, or None ), ioo writelines does not auto-convert to string and gives error - so the exact error message is important.

Now, looking at the program, it seems to be a program done in Python 3.x - since you treat the value returned from input as a string in line new = new.lower () '- and in that if all the elements of the array would be strings. It should not make a mistake - although I'm not sure if it will record what you want in the file.

Another thing is that in Python you do not normally use for with range as you do: for already returns each element of a sequence - then except the first for where you create its array, all others can be changed from something like:

for i in range(6):
    print(matriz[i])

for

for linha in matriz:
    print (linha)

There is no need for an index helper variable.

As I said, there are some tips - but without the error message you have, you can not tell why the program stops at that line specifically.

    
09.03.2016 / 14:13