繁体   English   中英

如何将 Python 用户输入格式化为时间,然后从该变量中减去?

[英]How to format Python user input as time then subtract from that variable?

我正在尝试为自己制作一个脚本,以帮助我围绕变化很大的工作时间表计划我的睡眠。

我想输入脚本:

  1. 当我计划上班时
  2. 如果我想早点上班,如果是的话:我想多早上班
  3. 我的预计运输时间将是多长时间
  4. 我需要多长时间才能“醒来”并准备工作
  5. 我想睡几个小时

期望的最终结果是脚本告诉我:

  1. 我应该几点睡觉 go
  2. 我应该几点起床
  3. 当我应该离开家去上班时,提前 x 分钟到达

我正在尝试在 Python 3 中阅读有关日期时间和时间的文档,但到目前为止一直很困难。 我目前写的数学不考虑将 60 分钟舍入到一个小时,所以我的结果不正确/不准确。

到目前为止我所拥有的显然不起作用,但应该说明我到目前为止的逻辑:

print("SLEEP CALCULATOR v0.1\n")

print("What time do you need to be at work?")
work_time = int(input("Enter time in 24h format (0500, 0800, 1400): "))

print("\nHow many minutes does it take you to get to work?")
transit_time = int(input())

print("\nWould you like to arrive at your workplace early?")
early_choice = input("Yes or No: ")
# how to tell if the user wants to arrive early (below)
while True:
    if early_choice == "yes" or "Yes":
        print("\nHow early would you like to arrive to your workplace?")
        early_time = int(input("In minutes: "))
        break
    elif early_choice == "no" or "No":
        break
    elif early_choice != "yes" or "Yes" or "no" or "No":
        print("\nPlease enter yes or no!")

print("How much time do you need to wake up and prepare for work?")
wake_time = int(input("(In minutes): \n"))

print("Lastly, how many hours of sleep do you desire?")
sleep_time = int(input("(In hours): \n"))

bed_time = work_time - (transit_time) - (early_time) - (wake_time) - (sleep_time * 60)
wake_up = work_time - (transit_time) - (early_time) - (wake_time)
leave_by = work_time - (transit_time) - (early_time)
work_arrival = work_time - (early_time)

print(
    "You need to go to bed at {}, wake up at {}, leave the house by {}, to get to work by {}!".format(
        bed_time, wake_up, leave_by, work_arrival
    )
)

欢迎来到堆栈溢出。

您将要查看 Python 的日期时间库

这是基于您的示例的快速演示:

work_time = datetime.strptime('2020-08-07 09:00:00', '%Y-%m-%d %H:%M:%S')
transit_time = timedelta(hours=1)
early_time = timedelta(hours=0) # Not arriving early
sleep_time = timedelta(hours=8)
wake_time = timedelta(hours=1)


bed_time = work_time - (transit_time) - (early_time) -  (sleep_time)
wake_up = work_time - (transit_time) - (early_time) - (wake_time)
leave_by = work_time - (transit_time) - (early_time)
work_arrival = work_time - (early_time)

print(
    "You need to go to bed at {}, wake up at {}, leave the house by {}, to get to work by {}!".format(
        bed_time, wake_up, leave_by, work_arrival
    )
)

在处理输入时,这里有一个如何完成的示例:

>>> work_time = work_time = datetime.strptime(('2020-08-07 ' + input("Enter time in 24h format (0500, 0800, 1400): ")), '%Y-%m-%d %H:%M:%S')
Enter time in 24h format (0500, 0800, 1400): 09:00:00
>>> print(work_time)
2020-08-07 09:00:00
>>>

这没有任何库,只有代码:)

print("SLEEP CALCULATOR v0.2 by Alex Zab \n")
    
work_time = int(input("What time do you need to be at work? \n Enter time in 24h format (05, 08, 14): \n"))
while True:
    if work_time > 24:
        print("Please enter time in 24h format (05, 08, 14): \n")
        work_time = int(input("What time do you need to be at work? \n Enter time in 24h format (05, 08, 14): \n"))
    else:
        break
transit_time = int(input("How many minutes does it take you to get to work? \n"))
while True:
    if transit_time > (work_time*60):
        transit_time = int(input("How many minutes does it take you to get to work? \n"))
    else:
        break
early_choice = input("Would you like to arrive at your workplace early? \n y or n: ")
while True:
    if early_choice == "y":
        print("How early would you like to arrive to your workplace?")
        early_time = int(input("In minutes: "))
        break
    elif early_choice == "n":
        early_time = 0
        break
    else:
        print("Please enter y or n!")
        early_choice = input("Would you like to arrive at your workplace early? \n y or n: ")

wake_time = int(input("How much time do you need to wake up and prepare for work? (In minutes): \n"))
while True:
    if wake_time > (work_time*60):
        wake_time = int(input("How much time do you need to wake up and prepare for work? (In minutes): \n"))
    else:
        break
sleep_time = int(input("Lastly, how many hours of sleep do you desire? (In hours): \n"))

bed_time = (work_time*60) - (transit_time + early_time + wake_time) - (sleep_time*60)
wake_up = (work_time*60) - (transit_time + early_time + wake_time)
leave_by = (work_time*60) - (transit_time + early_time)
work_arrival = (work_time*60) - early_time

print("You should arrive at " + str(work_arrival//60) + ":" + str(work_arrival%60) + " o'clock")
print("You need to leave house at " + str(leave_by//60) + ":" + str(leave_by%60) + " o'clock")
print("You need to wake up at " + str(wake_up//60) + ":" + str(wake_up%60) + " o'clock")
if work_time > 12:
    print("You need to go to bed at " + str(int(bed_time)//60) + ":" + str(int(bed_time)%60) + " o'clock")
else:
    bed_time = str(bed_time).replace("-", "")
    import math
    bed_time_hrs = 24 - int(bed_time)/60
    print("You need to go to bed at " + str(math.floor(bed_time_hrs)) + ":" + str(60 - int(bed_time)%60) + " o'clock")

不客气!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM