Header Ads

Header ADS

Draw Shape

This python program can draw few shape using *
Though it is not that much but if you are a new programmer then maybe you can feel it. 
at firsts I learn to draw triangle then one by one I had learned all of them. Then I thought why not make a combaind program, so here it is :
You can drew few thing like :
1-Diamond,
2-Square,
3-Triangle,
4-Parallelogram
5-Piramid

 

The code is :

def draw_shape(shape, size):
    if shape == 1:
        for i in range(1, size + 1):
            print(" " * (size - i) + "*" * (2 * i - 1))
        for i in range(size - 1, 0, -1):
            print(" " * (size - i) + "*" * (2 * i - 1))
    elif shape == 2:
        for i in range(size):
            print("* " * size)
    elif shape == 3:
        for i in range(1, size + 1):
            print("* " * i)
    elif shape == 4:
        for i in range(size):
            print("  " * (size - i) + "* " * size)
    elif shape == 5:
        for i in range(1, size + 1):
            print("  " * (size - i) + "* " * (2 * i - 1))
    else:
        print("Invalid shape selection.")

def main():
    while True:
        shape = int(input("Select a shape by number \n1-Diamond, \n2-Square, \n3-Triangle, \n4-Parallelogram\n5-Piramid \nor type 'exit' to quit: "))
        if shape == "exit":
            break
        elif shape in [1, 2, 3, 4, 5]:
            size = int(input("Enter size of the shape: "))
            draw_shape(shape, size)
        else:
            print("Invalid shape selection.")

if __name__ == "__main__":
    main()


some output :







 

No comments

Powered by Blogger.