Iteration problem in Python

python

#1

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:
0
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
The numbers:
0
1
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
The numbers:
0
1
2

The code is mentioned below

def w(n):
    i = 0
    numbers = []
    while i < n:
        print "At the top i is %d" % i
        numbers.append(i)
        #i = i + x
        count(i)
        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

w(6)

#2

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


#3

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


#4

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


#5

Thank you so much I got it.


#6

Good, you understand? No more questions?


#7

Yes, Sir! :slight_smile:


#8