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

Python Pattern Printing by accepting 5 Digit number as input

We were asked to write python program to read 5 digit number for example if the user entered 12345 the result should be :

1
12
123
1234
12345

I have written something like below but it’s not working and appending the output to earlier digit.

n = '12345'
for i in range(len(n)+1):
    for j in range(i+1):
         print(n[0:j],end = "")
    print()

The wrong output is

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

1
112
112123
1121231234
112123123412345

Please help in achieving the output in above format mentioned. Thanks in advance!

>Solution :

You can use

for i in range(1, len(n) + 1):
    print(n[:i])   

There’s no need for a nested loop here.

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