【问题标题】:Too many arguments for format string when using %d or .format [duplicate]使用 %d 或 .format 时格式字符串的参数过多 [重复]
【发布时间】:2021-03-06 09:44:03
【问题描述】:

我有这样的代码,但我不知道错误在哪里

x = int(input())

hh = x // 3600
second = x % 3600
mm = second // 60
ss = second % 60

print("%d remaining seconds is equal to %d:%d:%d".format(x, hh, second, mm, ss))

#Output
%d remaining seconds in equal to %d:%d:%d < I dont know why this code still %d and didnt change into numbers

当我使用print("%d remaining seconds is equal to %d:%d:%d" % (x, hh, second, mm, ss)) 时会出现错误not all arguments converted during string formatting

有什么建议吗?

【问题讨论】:

标签: python python-3.x


【解决方案1】:

为什么不使用 f 字符串而不是格式化,这将允许您将变量直接嵌入到字符串中。

print(f"{x} remaining seconds is equal to {hh}:{second}:{mm}:{ss}")

【讨论】:

    【解决方案2】:

    你需要把print这一行改成这一行

    print("{} remaining seconds is equal to {}:{}:{}:{}".format(x, hh, second, mm, ss))
    

    或

    print("%d remaining seconds is equal to %d:%d:%d:%d" % (x, hh, second, mm, ss))
    

    【讨论】:

      猜你喜欢
      • 2014-07-12
      • 1970-01-01
      • 2022-10-01
      • 2017-01-02
      • 2016-11-05
      • 2019-10-09
      • 2013-06-17
      • 1970-01-01
      • 2014-09-05
      相关资源
      最近更新 更多