Reputation: 11
I am trying to write a program in which a function accepts the name of a file and a list of tuples. Each tuple in the list will contain 5 entries, where the 1st, 3rd, and 4th entries are ints, 2nd entry is a string, and 5th entry is a float. The goal of the program is to write the 5 elements of each tuple on a separate line. So for example, all 5 from tuple 1 on one line, all 5 from tuple 2 on the second line, etc, in the file.
Right now, my code is giving me the exact output as what is expected by the test cases, but for some reason it says that my answer is incorrect. I am pretty sure that the issue is that my code is adding an extra space at the end of each line. How can I fix my code?
def write_1301(filename, tuple_list):
file = open(filename, "w")
for tup in tuple_list:
for i in range(0, len(tup)):
print(tup[i], file = file, end = " ")
print(file = file)
file.close()
#The code below will test your function. It's not used
#for grading, so feel free to modify it! You may check
#output.cs1301 to see how it worked.
tuple1 = (1, "exam_1", 90, 100, 0.6)
tuple2 = (2, "exam_2", 95, 100, 0.4)
tupleList = [tuple1, tuple2]
write_1301("output.cs1301", tupleList)
Example of a test case:
We tested your code with filename = "AutomatedTestOutput-MAZIxa.txt", tuple_list = [(1, 'quiz_1', 13, 20, 0.17), (2, 'exam_1', 55, 85, 0.12), (3, 'exam_2', 15, 20, 0.1), (4, 'assignment_1', 15, 30, 0.11), (5, 'test_1', 21, 35, 0.17), (6, 'quiz_2', 44, 70, 0.11), (7, 'test_2', 85, 100, 0.12), (8, 'test_3', 35, 50, 0.1)]. We expected the file to contain this:
1 quiz_1 13 20 0.17
2 exam_1 55 85 0.12
3 exam_2 15 20 0.1
4 assignment_1 15 30 0.11
5 test_1 21 35 0.17
6 quiz_2 44 70 0.11
7 test_2 85 100 0.12
8 test_3 35 50 0.1
However, the file contained this:
1 quiz_1 13 20 0.17
2 exam_1 55 85 0.12
3 exam_2 15 20 0.1
4 assignment_1 15 30 0.11
5 test_1 21 35 0.17
6 quiz_2 44 70 0.11
7 test_2 85 100 0.12
8 test_3 35 50 0.1
I tried using end= in various ways but had no luck and am confused.
Upvotes: 0
Views: 101
Reputation: 12199
Your issue is that your manually writting each element individually and adding a space after each element. This means that when you write the last element you also add a trailing space. so your output looks like it matches but acutally your lines all have a trailing space.
Instead you can use string join to join all the elements in the list with a space. This will add the space only between elements not after every element. However it expects all the values passed to it to be strings. So we can use a list comprehension so convert all the elements in the tup to strings and pass that list of strings to the join function.
def write_1301(filename, tuple_list):
with open(filename, "w") as file:
for tup in tuple_list:
print(" ".join([str(t) for t in tup]), file=file)
# The code below will test your function. It's not used
# for grading, so feel free to modify it! You may check
# output.cs1301 to see how it worked.
tuple1 = (1, "exam_1", 90, 100, 0.6)
tuple2 = (2, "exam_2", 95, 100, 0.4)
tupleList = [tuple1, tuple2]
write_1301("output.cs1301", tupleList)
Upvotes: 1