Python Forum
Need help getting the output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help getting the output
#1
S = input('Please enter something : ')

S = int(S)

T = S
for i in range(S+1):
    T = S - T
    for j in range(T):
            T = T - 1
            print(T, end='')
Output:
Please enter something : 5 4321043210432104321043210 Process finished with exit code 0
Output:
Expected output 12345 01234 00123 00012 00001
Please help me, how i can get this done
Reply
#2
aankrose Wrote:Please help me, how i have get this done
Try to print only the first line of output first.
Reply
#3
I agree try and make the inner loop that will print 12345, after you've nailed that you can go about altering it to create the other lines.

Try the following:
  • before a loop create a variable out_str and asign it as an empty string.
  • create a variable output_number, set it to 0.
  • create a loop that loops the required amount of times
    • add 1 to output_number
    • combine output_number to out_str
  • print out_str

Once you get that working
  • create an outer loop that loops same amount of times
    • previous code goes here but only add 1 to output_number if the second loop value is greater than or equal to the first loop value.
Reply
#4
S = int(input('Please enter something : '))
t=[]
for i in range(1,S+1):
    t.append(i)
for i1 in range(0,S):
    print(''.join(str(e) for e in t))
    for i in range(S-1,0,-1):
        t[i]=t[i-1]
    t[i1]=0
Reply
#5
Thank you skorost5 , i was breaking my head for 1 week. Hmmm .. Lot to learn.

Could you please explain below part?

for i1 in range(0,S):
    print(''.join(str(e) for e in t))
    for i in range(S-1,0,-1):
        t[i]=t[i-1]
    t[i1]=0
Reply
#6
Try and create your own code, that way you will learn more.
Try and create the sequence of code i posted above.
When you get stuck on any of it, post what you have so far and what the problem is and we can help you to create your own code.
Reply
#7
this string converts the string to a list:
print(''.join(str(e) for e in t))
output
Output:
12345
if I would write here so we will see:
print(t)
Output:
[1, 2, 3, 4, 5]
the cycle works in reverse order from the end to the beginning. 5 4 3 2 1
for i in range(S-1,0,-1):
t[i] equal to last element 5, а t[i-1] equal 4 and we will move the second-to-last element to last. this command is executed in a loop 5 time, moving each previous item to the next
t[i]=t[i-1]
this command unifies the first element. on the second round of the cycle resets the second element and so 5 time
t[i1]=0
Reply
#8
Awesome skorost5, Thank you
Reply
#9
As always there are different ways to achieve desired result. Following snippet is using built in string method str.zfill() combined with for loop and slicing:

>>> user_input = '5'                                               
>>> num = ''.join(str(x) for x in range(1, int(user_input) + 1))  # results '12345'
>>> for x in reversed(num):                                       # in reverse order 5->4->3->2->1
...     print(num[:int(x)].zfill(len(num)))                       # slice padded with zeros 
...
12345
01234
00123
00012
00001
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020