3

我是 Python 的初学者,所以我真的不知道很多术语或任何东西。我能问一下如何将分钟转换为小时和分钟吗?例如:75 分钟 -> 0 天,1 小时,15 分钟

print("Welcome to the Scheduler!")
print("What is your name?")
name = input()
print("How many chocolates are there in the order?")
chocolates = input()
print("How many chocolate cakes are there in the order?")
chocolate_cakes = input()
print("How many chocolate ice creams are in the order?")
chocolate_ice_creams = input()
total_time = float(chocolates) + float(chocolate_cakes) + float(chocolate_ice_creams)
print("Total Time:")
print("How many minutes do you have before the order is due?")
minutes = input()
extra_time = float(minutes) - float(total_time)
print("Your extra time for this order is", extra_time)

time = extra_time // 60

print("Thank you,", name)
4

5 回答 5

4

好吧,如果您的输入以分钟为单位大于等于 1440 分钟,那么您至少有一天的时间。因此,为了处理这个(以及时间的其他方面),我们可以使用模数 (%)。

days = 0
hours = 0
mins = 0

time = given_number_of_minutes   
days = time / 1440     
leftover_minutes = time % 1440
hours = leftover_minutes / 60
mins = time - (days*1440) - (hours*60)
print(str(days) + " days, " + str(hours) + " hours, " + str(mins) +  " mins. ")

这应该有效。

于 2016-01-26T14:21:43.857 回答
1

您实际上必须四舍五入才能获得整数。

import math

def transform_minutes(total_minutes):

    days = math.floor(total_minutes / (24*60))
    leftover_minutes = total_minutes % (24*60)
    
    hours = math.floor(leftover_minutes / 60)
    mins = total_minutes - (days*1440) - (hours*60)
    
    #out format = "days-hours:minutes:seconds"
    out = '{}-{}:{}:00'.format(days, hours, mins)
    return out
于 2021-06-21T13:03:54.180 回答
0
from datetime import datetime

day = minutes = hours = 0

day = datetime.now ()
day = day.day
minutes = datetime.now ()
minutes = minutes.minute
hours = datetime.now ()
hours = hours.hour


print ("It's day" + str (day) + "and it's" + str (minutes) + "minutes and" + str (hours) + "hours.")

于 2021-05-17T23:37:57.183 回答
0

上面的这段代码不起作用我开始制作我自己的版本,这个代码如何帮助你记住我是一个业余爱好者是真的

from datetime import datetime

day = minutes = hours = 0

time = datetime.now().minute
days = time / 1440
leftover_minutes = time % 1440
hours = leftover_minutes / 60
mins = time - (days*1440) - (hours*60)
print(str(days) + " days, " + str(hours) + " hours, " + str(mins) +  " mins. ")
于 2021-05-17T23:45:00.063 回答
-1
# Python Program to Convert seconds
# into hours, minutes and seconds
  
def convert(seconds):
    seconds = seconds % (24 * 3600)
    hour = seconds // 3600
    seconds %= 3600
    minutes = seconds // 60
    seconds %= 60
      
    return "%d:%02d:%02d" % (hour, minutes, seconds)
      
# Driver program
n = 12345
print(convert(n))


method = a
import datetime
str(datetime.timedelta(seconds=666))
'0:11:06'

method = b
def convert(seconds):
    seconds = seconds % (24 * 3600)
    hour = seconds // 3600
    seconds %= 3600
    minutes = seconds // 60
    seconds %= 60      
    return "%d:%02d:%02d" % (hour, minutes, seconds)

于 2021-05-18T00:08:41.067 回答