Python Forum
New to python and need help with spacing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to python and need help with spacing
#12
For me it smells like homework. So I provide one partial way to do it - how to print rectangle, but without center.

def do_rectangle(height=5, width=7, characters='GIS'):
    inner_width = len(characters) * (width - 2) + width - 1
    for i in range(height):
        if i in (0, height-1):
            print(' '.join(characters for i in range(width)))
        else:
            print(f'{characters}{" "*inner_width}{characters}')

do_rectangle()        
do_rectangle(height=5, width=5, characters='*')
Output:
GIS GIS GIS GIS GIS GIS GIS GIS GIS GIS GIS GIS GIS GIS GIS GIS GIS GIS GIS GIS * * * * * * * * * * * * * * * *
It's matter of defensiveness how to deal with negative width.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: New to python and need help with spacing - by perfringo - Sep-12-2021, 06:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Latex spacing question StillAnotherDave 0 1,474 Mar-28-2020, 05:51 PM
Last Post: StillAnotherDave

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020