I've just started learning Python and I'm running into a problem where I want to save an integer to use again if the program is shutdown. I've been looking around, but can't find anything about it. The code I've written so far:
from __future__ import print_function
collatz = open("collatz.txt", "w+")
Num = 5
calcNum = Num
while Num>0:
if calcNum % 2 == 0:
calcNum /= 2
print(calcNum, file = collatz)
else:
calcNum = (calcNum*3)+1
print(calcNum, file = collatz)
if calcNum == 4:
print("The infinite loop has been reached, moving on to the next number.", file = collatz)
Num += 1
print(Num, file = collatz)
calcNum = Num
I tried to save Num
into another file and then use that to keep it. However, it saves as a string instead of an int
, so I tried to use int()
which still didn't help.
Thanks in advance for any help.