Q:

Python - Generate Values On Demand

I'm having difficulties finding a solution to this problem, what i want to do is generate a value after a certain amount of time, so something like increase the value of 100.00 by .01 after 10 seconds but as it's been point out to me, in a for loop or while loop i cannot get the value until the iteration stops, so for instance if i have a variable with a number 100.00 and i want to increase or decrease that value by .01 
i can either do this:
import time
import datetime 
tracking = time.time()

def values():
    cost = 100.00
    increase = .01

    for x in range(1,1000):
        print(x)
        time.sleep(2)

        if time.time() - 10 > tracking:
            cost += increase
            print('Cost: {}'.format(cost))

            tracking = time.time()

    latestvalue = cost   

values()

okay so the loop does it's job it increases the value by 0.01 every 10 seconds but it's useless as i have no access to it, the latestvalue will only give me it's default which is 100.0
another solution that i've thought is to simply generate the values and put them on a list
import time
import datetime

increment = [100.01, 100.02000000000001, 100.03000000000002, 100.04000000000002, 100.05000000000003]

def values():

    for i in increment:
        newvalue = i
        time.sleep(10)
        print(newvalue)       

c = 0
while True:
    c+=1
    print(c)
    values()
    time.sleep(2)

but this way doesn't seem practical or reliable to me, could anyone help me with a solution? increase or decrease a value on demand after amount of time (10 seconds)

A:

You can use Timer() object from threading module. In this case, you can call the function over and over again, without needing to a while loop. For stoping the Timer(), cancel method is used.
from threading import Timer

def while_loop():
    t = Timer(1, while_loop)
    t.start()
    global variable
    variable-=0.1
    do_something()

def do_something():
    print(variable)

variable = 100
while_loop()

