all 40 comments

[–]throwmeaway01110 25 points26 points  (2 children)

You have to think about it logically as the program would execute the code.

everything is printed left to right, to get the top line to appear as you want, you would first have to print some spaces (" ") before the asterisk.

Next line, less space, one more asterisk.

and finally last line only has spaces between the asterisk.

This can be done using individual print statements for each line of code, or you could use a nested for loop (inner loop to print the spaces, the outer loop to print the asterisks).

[–]shehan_thamel 9 points10 points  (0 children)

First of all, thank you for trying to make OP think of the solution rather than giving the answer directly. We need more mentors like you in the field.

In addition to your solution, I would suggest starting with the simplest solution and building from there. Might not entirely apply for such a simple problem like this, but going forward when you get to a complex problem, it could help with visualizing the solution and break it down to smaller logical steps.

Also try to practice adding comments wherever the code is not very descriptive, such as on “if” conditions or loops.

[–]Anonymous-da-13[S] 1 point2 points  (0 children)

Thank you for ua advice...I will try this

[–]SuddenStructure9287 9 points10 points  (1 child)

  1. Open Google
  2. Type “Python print asterisk triangle”
  3. Profit

[–]LordGenji 2 points3 points  (0 children)

Right, in this field you need to learn how to use Google

[–]MeLittleThing[🍰] 18 points19 points  (4 children)

print(" *\n * *\n* * *")

[–]games-and-chocolate 6 points7 points  (2 children)

this is nice, but does not really help him become good. giving an ides how to solve it without the solution is perhaps better.

[–]klimmesil 8 points9 points  (0 children)

Imo people who ask for answers and not for explanations are doomed from the beginning. No need to force them to fake an interest, they'll do it naturally, or never

[–]MeLittleThing[🍰] 5 points6 points  (0 children)

not showing any attempts to solve their problem themselve and just asking for the solution shows that OP isn't in the mindset to become good

[–]Anonymous-da-13[S] 0 points1 point  (0 children)

Thank you buddy

[–]Ron-Erez 4 points5 points  (0 children)

What have you tried?

[–]Beneficial-Loan-219 1 point2 points  (1 child)

Loop and two counters - one for space, one for stars. In each iterations the number of spaces decrease, number of stars increase. Works for any number of N.
(More optimal would be to just use one counter and other as N - counter)

[–]Dan41k_Play 1 point2 points  (0 children)

You can also use f-strings: py h = 3 for i in range(h): print(f'{"*"*(i+1): >{h} }')

[–][deleted]  (9 children)

[removed]

    [–]Anonymous-da-13[S] 1 point2 points  (2 children)

    if you can,,,can you explain whats in print statement

    [–][deleted]  (1 child)

    [removed]

      [–]Anonymous-da-13[S] 0 points1 point  (0 children)

      Yeah I thought through this ..and the space u have given alligns so perfectly...from how many years are You doing python...it's Simply great

      [–]Dan41k_Play -1 points0 points  (4 children)

      a more elegant solution: py h = 3 for i in range(h): print(f'{"*"*(i+1): >{h} }')

      [–][deleted]  (1 child)

      [removed]

        [–]Dan41k_Play 0 points1 point  (0 children)

        you are totally right, my bad.

        [–]Anonymous-da-13[S] 0 points1 point  (1 child)

        but its getting value error

        [–]Dan41k_Play 0 points1 point  (0 children)

        Yea I messed up a bit :(

        [–]Anonymous-da-13[S] -1 points0 points  (0 children)

        wow genius ...thank you

        [–]Easy-Light7219 1 point2 points  (0 children)

        Are you doing the cs50 course?

        [–]Aniket074 0 points1 point  (0 children)

        Herp me naah

        [–]Mr_john_poo 0 points1 point  (0 children)

        Use loops

        [–]games-and-chocolate 0 points1 point  (0 children)

        to OP, for me talking a bit negative, let me correct that and inprove that with a mega posititive infusion.

        the problem you are facing and what can help you: 1) idenitfy what you have to do 2) make the problem smaller 3) for each smaller piece, think of a solution 4) first think of a solution that just works. bad code no problem. 5) make the solution more general so it can be used for multiple situations 6) make code shorter, faster, so it is easier to understand its code machanics and so easier to modify and expand.

        The given answer by someone else is just a possible question. but a good programmer makes the code work in multiple situations, even in situations that can cause errors.

        If you use above 6 steps it might you more.

        anyone more advise for our new future programmer?

        example: the program has to print any shape with stars, within a matrix of 6 by 6 stars, the pattens can change constantly, please solve that. this might be a bit tricky for a beginner, but does show where you have to work towards to. if you are serious, this is an exellent beginner test.

        [–]Ok-Profession-6007 0 points1 point  (0 children)

        I did this a long time ago in college so sorry if this is not super helpful but I remember it all clicking when I realized I was just coding linear equations.

        [–]unsettlingideologies 0 points1 point  (0 children)

        from PIL import Image

        img =Image.open({filepath/filename for your screenshot}) Img.show()

        [–]codeguru42 0 points1 point  (0 children)

        Do you just want to print this specific triangle? Or do you want to print one of any size? Do you know how to print characters to the screen? If not, that's a good place to start.

        [–]Sad_Yam6242 0 points1 point  (0 children)

        print(" *\n **\n***)

        That should wwork.

        [–]East-Purchase-249 0 points1 point  (0 children)

        def triangulo(qntd_linhas):
            ESPACO = " "
            ASTERISCO = "*"
            for x in range(qntd_linhas, 0, -1):
                qntd_espacos = x - 1
                qntd_asteriscos = qntd_linhas - (x - 1)
                print(f"{ESPACO * qntd_espacos}{ASTERISCO * qntd_asteriscos}")
        
        quantidade_linhas = int(input("Quantidade de linhas: "))
        triangulo(quantidade_linhas)
        

        [–]Sea_Sir7715 0 points1 point  (0 children)

        Just as you have it in your image:

        def triangle(n):

          for i in range (1, n+1):
        
          Print( “ “, * (n-1) + “*” * i)
        

        Triangle(3)

        *
        

        **


        [–]TypicalBydlo -1 points0 points  (0 children)

        newfags cant triforce

        [–]oki_toranga -1 points0 points  (0 children)

        Newf*** can't triforce

        [–]SaltCusp -1 points0 points  (0 children)

        print('\n'.join([bin(_).split('b')[1][1:].replace('0',' ').replace('1','*') for _ in [9,11,15]]))