Python f-string with variable width alignment and variable text

Viewed 244

I'm trying to randomize exam seating for different classrooms (in some classrooms one row can seat 4 students while in others one row can seat 3) and I drafted a Python script to print each student's name in the middle of each cell:

import random
import datetime

students = ['James', 'John', 'Robert', 'Michael', 'William', 'David', 'Richard', 'Charles', 'Joseph', 'Thomas', 'Christopher', 'Daniel', 'Paul', 'Mark', 'Donald', 'George', 'Kenneth', 'Steven', 'Edward', 'Brian', 'Mary', 'Patricia', 'Barbara', 'Linda', 'Elizabeth', 'Maria', 'Jennifer', 'Susan', 'Margaret', 'Dorothy', 'Lisa', 'Nancy', 'Karen', 'Betty', 'Helen', 'Sandra', 'Donna', 'Ruth', 'Sharon']
STUDENTS_PER_ROW = 4
LINE_WIDTH = 85
CELL_WIDTH = (LINE_WIDTH - STUDENTS_PER_ROW - 1) / STUDENTS_PER_ROW

seed = datetime.datetime.now()
print('Seed time is:', seed.strftime('%Y-%m-%d %H:%M:%S+%f'))
random.seed(seed.timestamp())
random.shuffle(students)

print('-'*LINE_WIDTH)
print(f"{'Whiteboard':^{LINE_WIDTH}}")
print('-'*LINE_WIDTH)
for i in range(0, len(students), STUDENTS_PER_ROW):
    for j in range(STUDENTS_PER_ROW):
        try:
            print(f"|{students[i+j]:^{CELL_WIDTH}}", end='')
        except IndexError:
            pass
    print('|')

What I got is:

Seed time is: 2022-05-28 12:57:03+969197
-------------------------------------------------------------------------------------
                                     Whiteboard                                      
-------------------------------------------------------------------------------------
|                    |                    |                    |                    |
|                    |                    |                    |                    |
|                    |                    |                    |                    |
|                    |                    |                    |                    |
|                    |                    |                    |                    |
|                    |                    |                    |                    |
|                    |                    |                    |                    |
|                    |                    |                    |                    |
|                    |                    |                    |                    |
|                    |                    |                    |

I tried to add {} around students[i+j], it didn't help. How can I fix this? Thanks for any help!

2 Answers

In this statement print(f"|{students[i+j]:^{CELL_WIDTH}}", end='') the data type of variable CELL_WIDTH is float which is supposed to be int ..

Change the statement CELL_WIDTH = (LINE_WIDTH - STUDENTS_PER_ROW - 1) / STUDENTS_PER_ROW with CELL_WIDTH = (LINE_WIDTH - STUDENTS_PER_ROW - 1) // STUDENTS_PER_ROW which will make CELL_WIDTH datatype as int..

I think you'd need to add int() to your statement because CELL_WIDTH is a float value. So:

CELL_WIDTH=int(LINE_WIDTH - STUDENTS_PER_ROW-1)/STUDENTS_PER_ROW

Hope that helps!

Related