I created this small python file to time how much time I spend practicing coding, it all worked perfectly until I attempted to turn all the functions I had into a class, once I did that I can't seem to get it to work, I have tried to add self
to the parameters of my functions and it hasn't worked. below is the code
import time
class Timer:
def time_convert(second, text):
minute = second // 60
second = second % 60
hour = minute // 60
minute = minute % 60
print(text, " = {0}:{1}:{2}\n".format(int(hour), int(minute), second))
def timer_calc( start_time, end_time):
time_lapsed = end_time - start_time
return time_lapsed
def file_writer(file_name, file_contents):
file = open(file_name, "w")
file.flush()
file.write(file_contents)
file.close()
def file_reader(file_name):
file = open(file_name, "r")
file_contents = file.read()
file.close()
return file_contents
def timer_main(file_name):
previous_time = file_reader(file_name)
input("Press Enter to start")
start_time = time.time()
input("Press Enter to stop")
end_time = time.time()
seconds_coding = timer_calc(start_time, end_time)
total_time = float(previous_time) + seconds_coding
file_writer(file_name, str(total_time))
time_convert(seconds_coding, "Current Coding Time")
time_convert(total_time, "Total Coding Time")
total_coding_time()
def total_coding_time(self):
python = file_reader("TimeSpentPython.txt")
tod = file_reader("TimeSpentTOD.txt")
cs50 = file_reader("TimeSpentCS50.txt")
total = float(python) + float(tod) + float(cs50)
time_convert(total, "The complete total coding hours are")
time.sleep(5)
def main(self):
while True:
user_input = input("what are you going to program on ? \npython (1)\n"
"The Odin Project (2)\nCS50 (3)\nOr Exit (4)\n")
if user_input == "1":
timer_main("TimeSpentPython.txt")
elif user_input == "2":
timer_main("TimeSpentTOD.txt")
elif user_input == "3":
timer_main("TimeSpentCS50.txt")
elif user_input == "4":
exit(0)
else:
print("please enter a valid choice")
if __name__ == "__main__":
timer = Timer()
timer.main()
The error shown in my IDE tells me that there are no such methods whenever I try to call a method from a different method inside the class.