Running a loop while a function is true in python
Question: I have a function which returns either `True` or `False`. I'm trying to
perform a loop where that function is called several times until it returns
`False`, and count how many times it ran.

    
    
    import random as rand
    
    def test_function():
       return rand.random > 0.5
    
    count = 0 
    
    while test_function():
       count += 1
    
    print count
    

All this is doing is running it once though, and holding whatever value it
got.


Answer: You forgot to actually call `rand.random`. Add `()` after it to do this:

    
    
    return rand.random() > 0.5
    

Below is a demonstration:

    
    
    >>> import random as rand
    >>> rand.random
    <built-in method random of Random object at 0x01DC02A8>
    >>> rand.random()
    0.4878133430124779
    >>>
    

Right now, your code is testing if the function object itself is greater than
`0.5`.

