我正在做一个程序,它应该在一个唤醒时间,你想醒来,然后它应该减去90分钟(1小时30分钟) 10次得到你应该睡觉的时间,例如如果我想在11:30醒来,那么它会给我10:00-8:30-7:00-5:30等等。你知道吗
下面是我的代码,最后一部分是由于我得到一个错误“replace()不接受关键字参数”而无法使用for循环的代码
import datetime
#Gets the time you would like to wake up at
user_input = input("Write a time you'd like to wake up at like \"11:30\": ")
#Cuts the first piece off so if it were 11:30, this would get 11
first_user_time = user_input[0:2]
first_user_time = int(first_user_time)
#Cuts the second piece off so if it were 11:30, this would get 30
second_user_time = user_input[3:5]
second_user_time = int(second_user_time)
#Gets the current time today
this_time = datetime.datetime.today()
#Replaces the time of today to your set "Wake up time"
new_time = this_time.replace(hour=first_user_time ,minute=second_user_time).strftime("%H:%M")
#Prints out your wake up time
print("Wake Up Time:", new_time)
#This one is not working for me and I need help with this one
#It's supposed to get 10 different times where you could go to bed to wake up fresh at "11:30"
for i in range(10):
the_hour = -1
the_minute = -30
sleep_time = new_time.replace(hour= the_hour, minute=the_minute).strftime("%H:%M")
print("Your Sleep Times:", sleep_time)
这是您
strftime
后面的字符串String
replace
不接受关键字参数所以,删除
strftime
,直到您真正想要显示值,然后问题是,正如cricket\u007所指出的,当关键字是字符串时,不能将关键字参数传递给
.replace()
。这适用于前面的.replace()
,因为这是一个日期对象,有一个替换小时、分钟等的方法。名称相同,但实际上有两个不同的函数。你知道吗你得到的另一个新问题是因为它试图将分钟设置为-30,这显然是不可能的。您应该使用
datetime.timedelta()
函数来执行此操作。你知道吗尝试以下更新:
将新的\u时间保留为datetime对象,以便可以正确地操作它
每次您想显示它时,请调用
strftime()
在循环中,使用
timedelta
对datetime
对象进行适当的减法,并在向用户显示时再次使用strftime()
。你知道吗相关问题 更多 >
编程相关推荐