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
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.