簡體   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