Home > other >  How do I Type Two Triangles on the Same Line
How do I Type Two Triangles on the Same Line

Time:10-06

I've been doing this project at school where we need to draw two diamonds side by side each other, and have its size dictated by user input.

I watched this video that told me how to draw one diamond, which is essentially two pyramid triangles, one with it's point facing up, and the other facing down.

The problem I'm struggling with is how do I type another two sets of triangles right beside the already existing two triangles, rather then being directly below them.

The video in questions where I saw how to make the first diamond shown a picture of what I want, which is called a double hill, but it doesn't go into detail in explaining how to make one.

I'm new to coding, and I've seen similar problems on this site getting answered, but those answers don't look like what I wanna do, so if anyone could spend some time helping me out with this, that'll be amazing.

My Code

diamond = int(input("Diamond size: "))

#Print first diamond
for i in range(diamond):
    for j in range(i, diamond):
     print(" ", end="")
     
    for j in range(i):
     print("*", end="")
     
    for j in range(i   1):
     print("*", end="")
    
    print()
    
for i in range(diamond):
    for j in range(i   1):
     print(" ", end="")
     
    for j in range(i, diamond - 1):
     print("*", end="")
     
    for j in range(i, diamond):
     print("*", end="")
    
    print()

Video I Watched https://www.youtube.com/watch?v=fX64q6sYom0

CodePudding user response:

the print() statement inserts a new line. So you just have to copy your for statement and paste them before the print() statement

This is the initial code I made which should solve your problem:

diamond = int(input("Diamond size: "))

#Print first diamond
for i in range(diamond):
    for j in range(i, diamond):
     print(" ", end="")
     
    for j in range(i):
     print("*", end="")
     
    for j in range(i   1):
     print("*", end="")


    # adding the same code here to make the lower triangle
    for j in range(i, diamond):
     print(" ", end="")


    # adding an additional for loop of spaces 
    for j in range(i, diamond):
     print(" ", end="")
     
    for j in range(i):
     print("*", end="")
     
    for j in range(i   1):
     print("*", end="")
 
    print()
    
for i in range(diamond):
    for j in range(i   1):
     print(" ", end="")
     
    for j in range(i, diamond - 1):
     print("*", end="")
     
    for j in range(i, diamond):
     print("*", end="")
    
    # adding the same code here to make the lower triangle
    for j in range(i   1):
     print(" ", end="")
     
    # adding an additional for loop of spaces 
    for j in range(i   1):
     print(" ", end="")

    for j in range(i, diamond - 1):
     print("*", end="")
     
    for j in range(i, diamond):
     print("*", end="")

    print()

results I am getting:

Diamond size: 5
     *          *
    ***        ***
   *****      *****
  *******    *******
 *********  *********
 *********  *********
  *******    *******
   *****      *****
    ***        ***
     *          *

You can make use of the print statement's function to escape from these for loops:

#Print first diamond
for i in range(diamond):
    print(" "*(diamond-i), end="")
    print("*"*i, end="")
    print("*"*(i 1), end="")

    # adding an additional for loop of spaces 
    print(" "*(diamond-i), end="")


    # adding the same code here to make the lower triangle
    print(" "*(diamond-i), end="")
    print("*"*i, end="")
    print("*"*(i 1), end="")


    print()
    
for i in range(diamond):
    print(" "*(i 1), end="")
    print("*"*(diamond-1-i), end="")
    print("*" * (diamond - i), end="")
    
     
    # adding an additional for loop of spaces 
    print(" "*(i 1), end="")

    # adding the same code here to make the lower triangle
    print(" "*(i 1), end="")
    print("*"*(diamond-1-i), end="")
    print("*" * (diamond - i), end="")

    print()

you should spend some time in optimizing your code but I don't know if you know about functions. If you do, try creating a function for creating upper and lower triangles

Update:

to have only one space in between diamonds just substract the additional spacing by 1

diamond = int(input("Diamond size: "))

#Print first diamond
for i in range(diamond):
    print(" "*(diamond-i), end="")
    print("*"*i, end="")
    print("*"*(i 1), end="")

    # adding an additional for loop of spaces 
    print(" "*(diamond-i-1), end="")


    # adding the same code here to make the lower triangle
    print(" "*(diamond-i), end="")
    print("*"*i, end="")
    print("*"*(i 1), end="")


    print()
    
for i in range(diamond):
    print(" "*(i 1), end="")
    print("*"*(diamond-1-i), end="")
    print("*" * (diamond - i), end="")
    
     
    # adding an additional for loop of spaces 
    print(" "*(i), end="")

    # adding the same code here to make the lower triangle
    print(" "*(i 1), end="")
    print("*"*(diamond-1-i), end="")
    print("*" * (diamond - i), end="")

    print()
  • Related