【问题标题】:not all arguments converted during string formatting并非所有参数都在字符串格式化期间转换
【发布时间】:2016-05-22 17:53:15
【问题描述】:

我正在学习 Python。在做练习时,我得到了错误

Python TypeError:字符串格式化期间并非所有参数都转换

我已经搜索了答案,但我不明白我找到的代码。

print "What's your name?",
name = raw_input()
print "What's your middle name?",
middle_name = raw_input()
print "Where do you live?",
country = raw_input()
print "Make of the car you drive?",
car = raw_input()
print "Are you single or married?",
socstatus = raw_input()
print "Are you Men or Woman?",
sex = raw_input()

print "Let me see if I understood correctly. Your complete name is %r %r, you live in %r, rides on a %r, and you are a %r $r." % (name, middle_name, country, car, socstatus, sex)

【问题讨论】:

  • 你没有告诉我们你遇到了什么错误
  • 您的最后一条打印语句以$r 结尾,而不是%r
  • 我认得那个代码;你漏掉了“酷”这个词。 :)
  • 是的!多亏了大家!

标签: python syntax


【解决方案1】:

这是一个简单的语法问题:您的最后一个格式规范似乎是 $r 而不是 %r。因此,您只有五个格式规范,但有六个变量。换最后一个。

print "Let me see if I understood correctly. Your complete name is %r %r,
you live in %r, rides on a %r, and you are a %r %r." % (name, middle_name,
country, car, socstatus, sex)

【讨论】:

    【解决方案2】:

    您使用 $r 而不是 %r 作为最终变量。

    【讨论】:

      【解决方案3】:

      格式字符串中有一个$r,而不是%r。但是,请记住,不推荐使用 %r 格式样式。您应该使用当前支持位置参数的 str.format()。

      # Get variables...
      print("Let me see if I understood correctly. Your complete name is {} {}, you live in {}, rides on a {}, and you are a {} {}.".format(name, middle_name, country, car, socstatus, sex))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-07
        • 2016-01-28
        • 1970-01-01
        • 2020-08-23
        • 2015-10-28
        • 2020-10-16
        相关资源
        最近更新 更多