Python 函数中相互不识别的方法
我创建了这个小python文件来计算我花在练习编码上的时间,在我尝试将我所有的函数都转换成一个类之前,它工作得非常好。一旦我这样做了,我似乎无法让它工作,我尝试将Python 函数中相互不识别的方法,python,class,methods,Python,Class,Methods,我创建了这个小python文件来计算我花在练习编码上的时间,在我尝试将我所有的函数都转换成一个类之前,它工作得非常好。一旦我这样做了,我似乎无法让它工作,我尝试将self添加到我函数的参数中,但它没有工作。下面是代码 import time class Timer: def time_convert(second, text): minute = second // 60 second = second % 60 hour = minut
self
添加到我函数的参数中,但它没有工作。下面是代码
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()
IDE中显示的错误告诉我,每当我试图从类中的另一个方法调用一个方法时,没有这样的方法。您需要更频繁地使用self。每个方法定义中都有一个self参数,调用每个方法之前都有一个self参数 -------------------------------------------------------------------------------
你没有课。你只有一组函数 以下代码中只有三件事发生了变化:
total\u coding\u time
中删除了self
参数main
如何调用静态方法:。另外,请在问题“在我的IDE中显示的错误”中包含准确的错误跟踪-请关闭防火墙,以便我们也能看到它。没有理由存在
计时器,因为计时器
不封装任何数据。所有的“方法”都应该是常规函数。没有理由仅仅为了让类工作而添加self
。如果您没有self
,则该方法应该是一个函数。如果所有的方法都应该是函数,那么您应该完全去掉这个类。
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(self, start_time, end_time):
time_lapsed = end_time - start_time
return time_lapsed
def file_writer(self, file_name, file_contents):
file = open(file_name, "w")
file.flush()
file.write(file_contents)
file.close()
def file_reader(self, file_name):
file = open(file_name, "r")
file_contents = file.read()
file.close()
return file_contents
def timer_main(self, file_name):
previous_time = self.file_reader(file_name)
input("Press Enter to start")
start_time = time.time()
input("Press Enter to stop")
end_time = time.time()
seconds_coding = self.timer_calc(start_time, end_time)
total_time = float(previous_time) + seconds_coding
self.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 = self.file_reader("TimeSpentPython.txt")
tod = self.file_reader("TimeSpentTOD.txt")
cs50 = self.file_reader("TimeSpentCS50.txt")
total = float(python) + float(tod) + float(cs50)
self.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":
self.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()
import time
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():
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__":
main()