Triangle in python


#1

whatsup guys, I'm a freshman software engineering student and I'm quite stuck on some homework.
Problem is, I understand the basics but I still really am a beginner, which is good so is why I can train to become better and better.
The question this is about is about making a triangle in a pyramid shape, so you'll have to work with spaces and "*"
The output is a pyramid shown in the dimension the user gave as input
For example, if input is 5 it outputs the following:

     *
    ***
   *****
  *******
 *********

So you may not use a multiple sign, so you can't code it like if x == 0 and y == 1 input = " " * input or something like that.
I was thinking in the first case about finding out what the middle of the grid is, using for loops and then building it further.
The only thing I'm getting stuck on is that I don't know how to tell my code that it has to loop until it reaches the input I need and adds 2 "*" and deletes 2 spaces everytime it loops.
My current code is the following

d = int(input("Dimension: ")
f = ""
for x in range(0, d+1):
    for y in range(0,d+1):
        if x == d / 2 and y == 0:
            f += "*" 
        if x == ? and y == d:

so I'm trying to say that it has to loop until it reaches y to be similar to d but idk how to work with that x...
So sorry if this is a dumb question and I really appreciate all kinds of help you guys show
Thanks <3


#2

@xmylittlemonsterx ,

How does the number that is input relate to the triangle? For an input of 5, you have cited the following output ...

 *
***

#3

Ah I see, it didn't work like it had to be,the output is the following:

     *
    ***
   *****
  *******
 *********

#4

wth it doesn't work?
well using '\n' then it outputs * '\n' *** '\n' ***** '\n' ******* '\n' *********
but there are spaces involved as well so that it creates some kind of triangle


#5

@xmylittlemonsterx ,

Could you post your most recent code?


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.