0

I need to write a tabular file that has the lists written so that it would come up as "Bob, 50, 45, 39", "Haley, 28, 27, 45", etc for the rest of the names, showing a report of their grades. So far, I know how to separate the names or numbers from one list, but I do not know how to combine the data.

students = ["Bob", "Haley", "Chris", "Nolan", "Asuka", "Cameron", "Tommy"]

midterm = [50, 28, 49, 90, 74, 0, 47]

essay = [45, 27, 76, 94, 0, 73, 67]

final = [39, 45, 65, 74, 36, 29, 90]

f = open ("student_data.txt", "w")

for i in students:
    f.write(str(i) + "\n")
f.close ()
Andrew T.
  • 4,701
  • 8
  • 43
  • 62

3 Answers3

0

You could do something like this

for i in range(len(students)):
  f.write(students[i] + str(midterm[i]) .... and so on)
  f.flush()
f.close()
John Hamlett IV
  • 495
  • 11
  • 23
0

zip is help you. just change print to write.

students = ["Bob", "Haley", "Chris", "Nolan", "Asuka", "Cameron", "Tommy"]
midterm = [50, 28, 49, 90, 74, 0, 47]
essay = [45, 27, 76, 94, 0, 73, 67]
final = [39, 45, 65, 74, 36, 29, 90]

z = zip(students,midterm,essay,final)

with open("student_data.txt", "w") as f :
      f.write("\n".join(",".join(map(str,x)) for x in z))
Ho0ony
  • 158
  • 11
0

If you must write a tabular file you may as well leverage the csv module:

f = csv.writer(open("student_data.txt", "w", newlines=""))
f.writerows(zip(students, midterm, essay, final))
donkopotamus
  • 22,114
  • 2
  • 48
  • 60