
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 (“*”) 
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 (“*”) 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).
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:
* * ** ** *** *** **** **** ***** ***** Source: https://ru.stackoverflow.com/questions/903820/
All Articles