Question
A function create_seating(file_name, rows, columns) that takes in a string which acts as a file name and two integers, the rows and columns of a
A function create_seating(file_name, rows, columns) that takes in a string which acts as a file name and two integers, the rows and columns of a resulting 2D list that you should return. The file contains student names, listed all on separate lines. You should take the student names and place them in the seats, filling an entire row before moving on to the next. If you reach the end of a file and there are no more seats, print "Not enough seats!", but still return the seating arrangement. For all empty seats, put an empty string. create_seating("students.txt", 2,3) [["Heidi", "Dylan", "Aaron"], ["Meredith", "Ben", ""]]
Assuming students.txt contained: Heidi Dylan Aaron Meredith Ben
def create_seating(file_name, rows, columns):
seats = []
file = open(file_name, 'r')
for i in range(rows):
row = [] for j in range(columns):
row.append(file.readline().strip(' '))
seats.append(row)
if len(file.readline()) != 0:
print("Not enough seats!")
return seats
Here is an review question and answer. Could you please go through every line and explain it please? I don't really understand the question and the process of adding the contents of the file to 2D list
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started