正在进行Python课程的作业,这需要将总分钟数分解为小时数和分钟数,而无需使用Datetime模块。我编写的代码可以正常工作,但是我希望将其缩短。如此简单的任务似乎有太多的代码。同样,我无法为此任务导入任何模块。
def total_minutes(mins):
hours = mins / 60 # how many hours
hours_rounded = (round(hours)) # round off the hours
rnd_hours_to_minutes = hours_rounded * 60 # multiply rounded hours and minutes
remaining_mins = mins - rnd_hours_to_minutes # separate remaining minutes from hours
if remaining_mins < 0: # have to have this statement because when minutes is between 30 and 60, shows negative number subtracting from 60
remaining_mins += 60 # adding 60 minutes to the negative number to reflect actual minutes
hours_rounded -= 1 # have to subtract hour since an hour is added when adding 60 minutes to negative number
vars_to_tuple = (hours_rounded, remaining_mins) # assign vars to tuple
print(vars_to_tuple) # will print hours and minutes in tuple; ex: (2, 59)
total_minutes(179)
def hours_and_minutes(mins):
hours = mins // 60
minutes = mins % 60
return hours, minutes