enter image description here

Help please write a range or while
My beginning, then I do not know what to do ..

 n = int (input()) for n in range( 0,n,1): Print (“*”) 
  • If you do not write this yourself, you will not understand, here the principle of the work of cycles (in all languages) - LFC
  • I’m already sitting around my head for an hour, at least pushing me ( - Sergey
  • Use the debager, once he helped me to understand this all, when I drove the cycle step by step. An hour is a bit, I spent almost a day on my time. - LFC
  • I edited and wrote from the phone, so it’s not convenient - Sergey
  • @vladspirin if a person came to this site for such a question, then a priori he does not want to learn how to program :) so why should he suffer) - Evgeny

2 answers 2

First, you see that the picture is symmetrical - let us first deal only with its left side.

There is one star in the first line, two in the second , three in the third , and so on. This means that the number of stars corresponds to the line number ( line_no in the subsequent program).

After the stars there are spaces . How many will there be? So much so that the stars plus the spaces are always the same number - say, twice the maximum number of stars (that is, the maximum number of lines the user specified): 2 * lines .

It follows that when the stars in the line_no line are line_no , then the spaces will be
(2 * lines - line_no) .

When we want to make a string of identical characters, for example, "AAA" we write 3 * "A" . Similarly, with some other character - in our case with an asterisk and a space.

This means that the first half of the line will be line_no * "*" + (2 * lines - line_no) * " " .
Well, the second - symmetrical half - the opposite.

And now the program:

 lines = int(input("Количество строк: ")) for line_no in range(1, lines + 1): left_part = line_no * "*" + (2 * lines - line_no) * " " right_part = (2 * lines - line_no) * " " + line_no * "*" print(left_part + right_part) 

for line_no in range(1, lines + 1): it means that the variable line_no will gradually take a value from 1 (inclusive) to lines + 1 (exclusively, that is, only to lines , what we want).

  • +1 for a detailed explanation, I think this answer will be useful to anyone who is looking for such. To this question we will duplicate. - LFC
  • Thank you, @VladSpirin. :-) - MarianD

Here is an example of a solution to clarify the logic:

 gap = 20 # ширина строки целиком for i in range(1,6): # ange(1,6) - итого 5 - количество строк print('*'*i+' '*(gap-i*2)+'*'*i) # печать необходимого количества звезд слева, # необходимого количества пробелов, необходимого # количества звезд справа - всё в зависимости # от номера текущей строки (i). 

At the exit:

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