samedi 19 octobre 2019

How to Write Two Dimensional List to file

I'm currently trying to write a program where after the values taken from a User input for a Golf Data Sheet would have each record written to file with their Names and Statistics stored as separate individual records. Unfortunately I've run into a roadblock where I cannot get these to store to file. What would I need to enter?

This is for a new Python File, in which the file would be storing the data. I can verify I have managed to make the program already store each field to an individual record with the information, however I cannot seem to have it all be written to a file, somehow.

golf_data = []
run_loop = 0

while run_loop != 16:
      main_data = []
      inner_loop = 0
      while inner_loop != 1:
          fname = input("Entry First Name: ")
          lname = input("Entry Last Name: ")
          handicap = float(input("Handicap value: "))
          score = float(input("Enter the Score: "))
          main_data.append(fname)
          main_data.append(lname)
          main_data.append(handicap)
          main_data.append(score)
          golf_data.append(main_data)
          inner_loop += 1
run_loop += 1


outfile = open('golf.txt', 'w')
for element in golf_data:
       outfile.write(element + '\n')

outfile.close()

I expect the file to store the results inputted by the User, however instead I keep getting this error:

Traceback (most recent call last):


File "C:/Users/joelc/PycharmProjects/Project2/File/JoelLab5Pt1.py", line 24, in <module>
     outfile.write(element + '\n')

TypeError: can only concatenate list (not "str") to list

Aucun commentaire:

Enregistrer un commentaire