如何将这些组合成一个'msg'变量?

时间:2017-05-01 00:30:49

标签: python typeerror python-3.5

目前我将这些分解为3个单独的'msg'。 但我需要它是一个'msg'。

    def dateDiffInSeconds(date1, date2):
      timedelta = date2 - date1
      return timedelta.days * 24 * 3600 + timedelta.seconds

    def daysHoursMinutesSecondsFromSeconds(seconds):
        minutes, seconds = divmod(seconds, 60)
        hours, minutes = divmod(minutes, 60)
        days, hours = divmod(hours, 24)
        return (days, hours, minutes, seconds)

    injustice2 = datetime.strptime('2017-05-15 23:01:00', '%Y-%m-%d %H:%M:%S')
    fridaythe13th = datetime.strptime('2017-05-25 23:01:00', '%Y-%m-%d %H:%M:%S')
    shadowofwar = datetime.strptime('2017-08-21 23:01:00', '%Y-%m-%d %H:%M:%S')

    now = datetime.now()

    msg = (
    '**%d** days, **%d** hours, **%d** minutes, and **%d** seconds until the release of **Injustice 2**.'
    ) % daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now, injustice2))

    msg2 = (
    '**%d** days, **%d** hours, **%d** minutes, and **%d** seconds until the release of **Friday the 13th: The Game**.'
    ) % daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now, fridaythe13th))

    msg3 = (
    '**%d** days, **%d** hours, **%d** minutes, and **%d** seconds until the release of **Middle-earth: Shadow of War**.'
    ) % daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now, shadowofwar))

我试过这个,但后来我得到“TypeError:没有足够的格式字符串参数”。我知道这必须是一个我不太了解的简单问题。任何帮助将不胜感激。

msg = (
'**%d** days, **%d** hours, **%d** minutes, and **%d** seconds until the release of **Injustice 2**.'
'**%d** days, **%d** hours, **%d** minutes, and **%d** seconds until the release of **Friday the 13th: The Game**.'
'**%d** days, **%d** hours, **%d** minutes, and **%d** seconds until the release of **Middle-earth: Shadow of War**.'
) % daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now, injustice2)), daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now, fridaythe13th)), daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now, shadowofwar))

2 个答案:

答案 0 :(得分:1)

您必须合并daysHoursMinutesSecondsFromSeconds生成的元组。

% (daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now, injustice2)) + daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now, fridaythe13th)) + daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now, shadowofwar)))

另请注意封闭的()

如果没有这个,你发送一个大小为3的元组,其中包含从daysHoursMinutesSecondsFromSeconds返回的值:

(14, 20, 5, 6), 
(24, 20, 5, 6),
(112, 20, 5, 6)

这被认为是3个值,而不是12个。

当我们将元组与+合并时,它将是正确的:

(14, 20, 5, 6, 24, 20, 5, 6, 112, 20, 5, 6)

另请注意,当您遇到此类问题时,print是您的朋友。

答案 1 :(得分:1)

msg =( ' %d 天,%d 小时,%d 分钟,以及%d 秒发布不公正2 。' ' %d 天,%d 小时,%d 分钟,以及%d 秒发布星期五13日:游戏。' ' %d 天,%d 小时,%d 分钟,以及%d 秒释放中土世界:战争阴影。' )%(daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now,injustice2))+ daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now,fridaythe13th))+ daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now,shadowofwar)))