【问题标题】:Convert year to seconds in Python在 Python 中将年份转换为秒
【发布时间】:2015-05-02 00:59:23
【问题描述】:

我被困在一个需要我转换输入年份并在几秒钟内打印结果的任务上。运行该函数时收到的错误消息是:

'*TypeError: not all arguments converted during string formatting*'

我实在想不通到底出了什么问题……

def ageInSeconds():
    """
    Reads the age and converts to seconds.
    """
    ageYears = int(input("Tell me your age, please: "))
    seconds = ageYears * (365*24*60*60)
    print("\nBatman says you age in seconds is: " % seconds)

【问题讨论】:

  • 字符串中没有% 用于替换seconds...
  • 如果您询问生日,您可以获得更精确的值,例如,从今天到 1978 年 3 月 1 日(生日)之间有 13514 天,因此从那时起经过了13514*86400 + 18 SI 秒(同时日(考虑到与两个日期相对应的 UTC 偏移量的可能差异))。 18 是自 1978 年 3 月 1 日以来的闰秒数。

标签: python python-3.x time


【解决方案1】:

字符串中缺少%d

print("\nBatman says you age in seconds is: %d" % seconds)
                                             ^

你可以这样做

print("\nBatman says you age in seconds is:",seconds)  # Inbuilt feature of the print function

【讨论】:

  • 非常感谢。感谢您的帮助。
  • @ThomasBengtsson 未来一切顺利:)
猜你喜欢
  • 2018-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
  • 2010-12-11
  • 2017-11-22
  • 1970-01-01
  • 2019-06-04
相关资源
最近更新 更多