I wrote the following code but after executing the code, it gives correct output for for two rows but gets stuck later. what’s going wrong here?? i=3
j=0
while(i>1):
a="10"*i+"1"+" "*j
print a
i=i-1
j=j+2
if(i<=1 and j>=4):
break
i=1
j=1
while(i<=1):
a=" "*j+"10"*i+"1"+" "*j
i=i-1
j=j+2
See https://www.codesdope.com/discussion/while-exercise/ for the correct answer. This is the exactly same question.
About your code – a="10"*i+"1"+" "*j – You need to add spaces before the string also – a=" "*j+"10"*i+"1"+" "*j
Also, I don’t see the use of this block of code:
i=1
j=1
while(i<=1):
a=" "*j+"10"*i+"1"+" "*j
i=i-1
j=j+2
hello my friend, I'm Shubhendu! From what I see, your loop will break while printing 3rd pattern because of the if stmt which is if(i<=1 and j>=4) you can verify it by doing it in paper pencil method. Also i have modified your code to look same to same as given in question of while loop exercise. But also Amit Kumar's answer is right too,but its for more organised pattern display. Now if you want same to same pattern,it will be like:-
i=3
j=0
while(i>=0):
a="10"*i+"1"+" "*j
print a
i=i-1
j=j+2
if(i<0 and j>=4):
break
i = 7
s = 0
k=3
while (i>=0 and k>=0):
a=(" "*s+"10"*k+"1"+" "*s)
print(a)
k=k-1
i=i-2
s=s+1
Hello All I have solution for this please try below code. while writing code check intendation
num=int(input("Enter Number"))
k=num-2
for i in range(num,0,-1):
a=(i*2)-1
for j in range(k, 0,-1):
print(end=" ")
k = k +1
for j in range(0,a,1) :
print(end="")
if j %2==0:
print(1,end=" ")
else:
print(0,end=" ")
print()