while loop (Reposting On Python-List Prohibited)

BartC bc at freeuk.com
Tue Oct 11 18:23:33 EDT 2016


On 11/10/2016 22:26, Lawrence D’Oliveiro wrote:
> On Wednesday, October 12, 2016 at 6:58:46 AM UTC+13, dhawan... at gmail.com wrote:
>> Only first loop is executing not the second one?
>
> 	n=6
> 	x=1
> 	while x<=n:
> 		print("*"*x)
> 		x+=1
> 	print('n=', n)
> 	print('x=', x)
> 	while n>=x:
> 		n=n-1
> 		print("*"* n)
>
> 	*
> 	**
> 	***
> 	****
> 	*****
> 	******
> 	n= 6
> 	x= 7
>
> Moral: always use debug print statements to figure out what is going on.
>

'else' can be used here:

n=6
x=1
while x<=n:
     print "*"*x
     x+=1

while n>=x:
     n=n-1
     print "*"* n
else:
     print ("2nd loop exit n=",n,"x=",x)

-- 
Bartc



More information about the Python-list mailing list