0

So I'm trying to create the letter H out of asterisks using Python, but I can't seem to figure out on how to create a vertical line to go across in order to create the letter H using the code I have written so far:

def across(n):    
    for i in range(n):
        print ('*', end=' ')
    print()

def straight(n):
    for i in range(n):
        print ('*')

def main():
    n=6
    straight ((n-3)//2)
    across(n//2)
    straight ((n-3)//2)

main()

this is the output:

*
***
*

Any ideas? Thanks for any help in advance.

1 Answer 1

0

You have to print one line at a time. Once you have switched to the next line you cannot return to the previous line. So, the idea is (e.g. for height 3), on the first iteration the code should print "star star". It should change to the next line ('\n') and then print "starstarstar" since it is the middle line. And finally, on the last line "star star" again. Hope this helps.

def line(a):
    print "*",

    for j in range(a-2):
        print " ",

    print "*",

def middleLine(a):
    for j in range(a):
        print "*",

a = int(raw_input("Enter height (odd numbers only greater than 3): "))

for i in range(a):
    if i != (a / 2):
        line(a)

    else:
        middleLine(a)

    print

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.