My goal is to be able to run a function and print the result every second. I want it to look something like this:
printing:
"At {seconds} you have {value}." // Where the value comes from another function we can also assume that the value goes up by something constant, but is calculated by another function.
And to continue printing this on new lines for each whole second. I have come across two problems so far.
1) I am unable to write a function that prints a value every second. I've tried using threading and time, but I haven't been able to get it work.
2) If I set a sleep timer for 1 second, it must take several milliseconds (or way smaller time measurements) to then calculate the function before it prints the above line. I figured that after several hundred/thousand calculations, my time and values would no longer be accurate.
So far, I have borrowed this from another thread:
def update(i):
threading.Timer(1, update, [i]).start()
sys.stdout.write(str(i)+'\r')
sys.stdout.flush()
print(i)
i += 1
This function doesn't work how I imagined. I thought that it would up the value of i by one each time, and put that value back in the update() function. It just keeps printing out the same value each time. Honestly, I'm not sure why, and I tried going line by line to figure it out, but alas I cannot.
Thanks
-2.0