Iteration problem in Python

I want to call a function from a function in python. I tried executing this code the out results in a wrong way

This isn’t the exercise from CodeAcademy but another book

Please provide me an answer am I wrong in any logic?

Results continous numbers of ‘0’

If n = 3, then the expected output is

At the top i is 0
Numbers now: [0]
At the bottom i is 1
The numbers:
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
The numbers:
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
The numbers:

The code is mentioned below

def w(n):
    i = 0
    numbers = []
    while i < n:
        print "At the top i is %d" % i
        #i = i + x
        print "Numbers now: ", numbers
        print "At the bottom i is %d" % i
        print "The numbers: "
        for num in numbers:
            print num
def count(i):
    i = i + 1
    return i


A little warning about an infinity loop would have been nice

why did you make a separate count function? You could simply do i = i + 1 in the while loop of your w function

anyway, i is not increased because you don’t store the returned result by count function anywhere


I wanna simply add a function ( just trying it). You mean like x = count(i)

close, in this case we want to store the returned result in i given that is what we use in our while loop condition


Thank you so much I got it.

Good, you understand? No more questions?

Yes, Sir! :slight_smile: