【问题标题】:Taking time as input in Python在 Python 中将时间作为输入
【发布时间】:2018-10-14 17:41:24
【问题描述】:

我的意思是小时和分钟。将它们作为 input() 函数的参数并将其分配给变量。有可能吗?

像这样:

time = input(current time)

【问题讨论】:

标签: python


【解决方案1】:

如果您需要分别使用小时和分钟

from datetime import datetime
a=datetime.now()
print(a.hour,a.minute)

【讨论】:

    【解决方案2】:

    intput 只返回str(字符串)类型的对象。

    但是,您可以通过指定某种格式并使用标准库中的datetime 执行转换来发挥创造力:

    from datetime import time
    
    time = input('Enter a time in the format HH:MM \n')
    # input 15:25
    
    h, m = map(int, time.split(':'))
    res = time(hour=h, minute=m)
    
    print(res)
    
    15:25:00
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2023-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多