Nested Loop Code Help

Richard Damon Richard at Damon-Family.org
Sun Jan 26 19:53:37 EST 2020


On 1/26/20 6:52 PM, DL Neil via Python-list wrote:
> On 27/01/20 4:15 AM, ferzan saglam wrote:
>> Hello people, I have written the code below which works fine, but it 
>> has one small problem. Instead of printing one (x) on the first line, 
>> it prints two.
>> I have tried everything in my knowledge, but cannot fix the problem.
>> Thanks for any help in advance.
>> for x in range ( 0, 10):
>>    stars = 'x'
>>    count = 0
>> while count < x:
>>    stars = stars + 'x'
>>    count = count + 1
>>    print (stars)
>
> These loops are serial, ie one after the other, and not "nested" (one 
> 'inside' the other) - or the email messed-up the indentation.
>
> However, why "nest" or have more than one loop, in any case?
>
> >>> for i in range( 0, 10 ):
> ...     print( "*"*i )
> ...
>
> *
> **
> ***
> ****
> *****
> ******
> *******
> ********
> *********

First, your answer doesn't solve his problem, as his expected was lines 
of 1 to 10 stars, not 0 to 9.

Second, this smells a bit like homework, and if they haven't learned the 
results of string times integer, then using that operation wouldn't be 
in their tool kit, so having a loop to build that operator makes sense.

-- 
Richard Damon



More information about the Python-list mailing list