Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to output this template in Python

How to output this template in Python

Can this be done with the for loop?

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading