Drew Thomas
Drew Thomas

Reputation: 1

Python - How do I keep returning values from a function with a while statement?

So, the code outlined below sends arguments to a function I've created called bsearch and I want the function main() to send the arguments with the key argument scaled down by 1 from 11 (11,10,9,8,7...) until it reaches 0 an I want the value count outputted each time --- currently it only returns the first count. How do I get it to return after each while loop?

def main():
    ilist = [x+1 for x in range(10)]
    key = 11
    start = 0
    end = 10
    while key > 0:
        count = b(ilist,key,start,end)
        key = key -1
        return count

Upvotes: 0

Views: 1387

Answers (1)

zsoobhan
zsoobhan

Reputation: 1524

I think you might want to look at a few tutorials but I imagine you want something like this:

def main():
    count_list = []
    for x in range(1,11):
        count_list.append(bsearch(x))  # append your results to a list
    return count_list  # return out of the scope of the loop

or using a list comprehension as suggested in the comments:

def main():
    return [bsearch(x) for x in range(1,11)]

Upvotes: 2

Related Questions