【问题标题】:Getting time of day from user for silly simple app从用户那里获取愚蠢的简单应用程序的时间
【发布时间】:2019-10-22 23:03:19
【问题描述】:

我是 Python 初学者,我想尝试让 myslef 成为一段简单的代码,它可以在我上班时输入我的时间并告诉我什么时候回家。

问题是输入格式,然后将这 8 个小时的工作添加到其中并产生输出。

我可以让它与整数一起工作,但这还不够好。我想在 I/O 中看到 HH:MM 格式

我在网上搜索了所有内容,但我发现的主要是关于一般时间的文章和建议(日期,更重要的是 datetime.now),而我需要它来了解我想在上班时输入时间和什么时候我可以离开工作,不管是什么日子等等。

# When do I get to go home program
# first we need ot define a variable to use it to store the time when I came 
# to work
# That is atw (arrived to work)
# gh is "go home"

from datetime import time
atw = input ("When I came to work in format of HH:MM \n")
#atw = datetime.strptime(input('enter time you arrived to work in HHMM format: '), "%H%M")
h,m = map (int, time.split(":"))
#gh = atw (hour = h, minute = m)
print (("I came to work @", atw ,"%H%M"))

gh = atw + 0800  
print ("You can go home @ ", gh)

各种错误消息,从语法到我的输入格式问题以及将这 8 个小时的工作添加到最终数字(gh 变量)

是否有一些很棒的存在可以引导我了解如何真正让 python 理解我正在向变量添加时间,该变量应该格式化为来自用户的时间 (HH:MM) 输入?

【问题讨论】:

  • 使用strptime有什么问题?这似乎是一种合理的方法。
  • Traceback(最近一次调用最后一次):文件“..\Playground\”,第 9 行,在 atw = datetime.strptime(input('输入您到达工作的时间,以 HHMM 格式: '), "%H%M") NameError: name 'datetime' is not defined 如果这很愚蠢,我很抱歉,但我可能只是不明白如何正确输入或使用它
  • 你必须要么使用import datetime,然后使用datetime.strptime,要么from datetime import strptime,然后使用strptime(没有datetime.)。

标签: python-3.x input time formatting


【解决方案1】:

好的,让我们一步一步来,因为正如你所说,那里各种错误消息,从语法到...

from datetime import time
atw = input ("When I came to work in format of HH:MM \n")

前两行没有问题,没问题。

h,m = map (int, time.split(":"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'datetime.time' has no attribute 'split'

哦,一个错误,让我们看一下错误消息的内容:type object 'datetime.time' has no attribute 'split'。嗯,没错,因为我们不想拆分time,但是我们给输入的时间名称为atw,所以让我们使用它:

h,m = map (int, atw.split(":"))
print (("I came to work @", atw ,"%H%M"))

没有错误,但输出很奇怪:

('我来上班@', '08:17', '%H%M')

您可能不希望将元组作为输出(括号)并希望格式化该输出,正如我从格式字符串 "%H%M" 中看到的那样。幸运的是,python (>=3.6) 有一个很好的解决方案,叫做f-strings:

print (f"I came to work @{atw}")

嗯,等等,格式怎么样,它只是输出我们输入的内容......所以使用h和m:

print (f"I came to work @{h:02d}:{m:02d}")

用谷歌搜索:02d 的含义以及还有哪些其他选项;) 下一行:

gh = atw + 0800  
  File "<stdin>", line 1
    gh = atw + 0800  
                  ^
SyntaxError: invalid token

嗯,你想添加 8 小时来获得结束时间,但你得到的只是一个语法错误......那是因为 0800 不是有效的 Python 构造,你可能只输入了 8 小时?让我们试试这个......

gh = atw + 8  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

另一个错误,但为什么?您正在使用 atw,它是一个字符串,尽管您已经将其拆分为两个整数 h 和 m,其中 h 代表小时:

gh = h + 8
print ("You can go home @ ", gh)

啊,该死的又是那个印刷品。查看上方并使用 f-strings:

print (f"You can go home @{gh}:{m}")

现在尝试看看你是否得到了使用 strptime 的解决方案,正如 cmets 中建议的 @mkrieger1。 并请做一个 python 教程:)

【讨论】:

  • 我感激不尽,已经用 Chuck Severance 全力以赴进入 P4E 了,太棒了,只是一点点学习,我想这个时间格式有点太复杂了,但是这确实有助于理解它的含义。再次感谢您的帮助,祝您有美好的一天!
猜你喜欢
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 2018-10-07
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 2016-03-22
相关资源
最近更新 更多