Python Forum
Blank spaces in right side
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Blank spaces in right side
#1
Hello, I'm new to Python and i had a class assignment that was to draw figures as contours of a regular hexagon and of rectangles and isosceles triangles. I was able to make the necessary patterns (and gave a hard job to me hahahaha :/), but I forgot that I could not leave blanks to the right of the figure, i.e each line must end as soon as the last character is printed on this line, and couldn't think of a way to do it, could someone give me a light on it? I thought of using concatenated strings or something to limit the print on each row, but i couldn't develop it :/

This is the code for the hollow hexagon in function of variable "ladoqh" that is the side of the hexagon and variable "letrinha" that is the character that is to be printed as the hollow hexagon:
ladoqh=int(input())
letrinha=str(input())
totalhexagono=2*(ladoqh-1)+ladoqh
        controlehex = 1
        while controlehex<=totalhexagono :
            if controlehex==1 or controlehex==totalhexagono:
                print (" "*(ladoqh-1) + letrinha*ladoqh + " "*(ladoqh-1))

            if controlehex>ladoqh-1 and controlehex<= 2*ladoqh-ladoqh:
                print (letrinha + " "*(totalhexagono-2) + letrinha)

            if controlehex>=1 and controlehex<ladoqh-1:
                print (" "*(ladoqh-1-controlehex) + letrinha +  " " *((ladoqh-2) + 2*(controlehex)) + letrinha + " "*(ladoqh-1-controlehex))

            if controlehex>=2*ladoqh-1 and controlehex<totalhexagono-1:
                print (" "*(controlehex-totalhexagono+ladoqh) + letrinha +  " "*(totalhexagono-2*(controlehex-totalhexagono+ladoqh+1)) + letrinha + " "*(ladoqh-1-controlehex))
            controlehex+=1
        print()
Thanks in advance for any help :)
Reply
#2
line 2: input() returns a string so no need to convert string to string using str()
line 5: i wouldn´t use a while-loop here but a for-loop
line 6 to 15: there are four if-statements and that are four too much, it can be done without an if-statement

I hope you using python >= 3.6 if not i would advise to use it because then you can use f-strings for creating the lines
an example for the first line:
f'{(ladoqh-1)*" "}{letrinha}{(ladoqh-2)*letrinha}{letrinha}'
If you look at a hexagon:
Output:
**** * * * * * * * * * * ****
the first line and the last one are the same, the second line and the line before the last are the same, etc ....
So you only need to create the first line up to the middle line and can reuse the lines before in reverse order.
line1
line2
line3
middle
line3
line2
line1

here´s the f-string for line2 up to middle inclusive
f'{(ladoqh-i)*" "}{letrinha}{(ladoqh+(i-2)*2)*" "}{letrinha}'
Now create a for-loop around it, append the already created lines in reverse order and print them.
Good luck.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Seven Segment Display - QUERY on Multi-line side-by-side printing output ajayachander 3 4,830 Mar-13-2020, 07:02 AM
Last Post: ajayachander
  exception in side if clause sbabu 6 2,969 Feb-15-2020, 08:44 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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