I can't make the last line spread across in my display

37 views Asked by At

I have the following code to print a schedule from inputs but can't make the last line spread under the like the header does.

My showRecord code is as follows...

Class Appointment:
    def __str__(appt):
        return f'{appt.date} {appt.start} {appt.end} {appt.description} {appt.venue} {appt.priority}'

OTHER CODE UNTIL

def showRecords(schedule):
    print('{:<15}{:<10}{:<8}{:<25}{:<25}{:<10}'.format("Date","Start","End","Subject","Venue","Priority")) # '<' here aligns text to left
    print('{:<15}{:<10}{:<8}{:<25}{:<25}{:<10}'.format("----","-----","---","-------","-----","--------"))
    for appointment in schedule:
        print(appointment)
    else:
        print("No more appointments found.")

I have tried the following

print('{:<15}{:<10}{:<8}{:<25}{:<25}{:<10}'.format(schedule))

...and...

print('{:<15}{:<10}{:<8}{:<13}{:<11}{:<10}'.format(date, start, end, description, venue, priority))

but the last line just bunches up as follows...

enter image description here

I've tried several codes as noted above and can't make it work. Please help.

NOTE: I've added the rest of the code!

1

There are 1 answers

0
MatBailie On

You can't just rely on __str__, because it (correctly) doesn't have any padding or alignment codes in the string formatting.

Nor can you rely on the padding and alignment of the headers, because that's only for the headers.

So, if you want to pad and align the text representation of an appointment, you have to do it yourself. By adding the padding and alignment formatting codes to the string being formatted and printed...

def showRecords(schedule):
    print('{:<15}{:<10}{:<8}{:<25}{:<25}{:<10}'.format("Date","Start","End","Subject","Venue","Priority")) # '<' here aligns text to left
    print('{:<15}{:<10}{:<8}{:<25}{:<25}{:<10}'.format("----","-----","---","-------","-----","--------"))
    for appt in schedule:
        print(f'{appt.date:<15}{appt.start:<10}{appt.end:<8}{appt.description:<25}{appt.venue:<25}{appt.priority:<10}')
    else:
        print("No more appointments found.")