How to output this template in Python
Can this be done with the for loop?
This is my experiment so far
num = int(input())
for item in range(0,num+1):
for i in range(item,0,-1):
print(i,end=' ')
print("\r")
>Solution :
If you want to output like the screenshot, there are multiple approaches to this. such as, you can do something like this.
Here outer loop controls the number of lines and the inner loop iterates through powers of 2 and prints them.
n = 8
for i in range(n):
j = 2**i
while j >= 1:
print(j, end='\t')
j = j//2
print()
If you want to achieve it only with for loop you can do this like:
for i in range(n):
for j in range(i, -1, -1):
print(2**j, end='\t')
print()
