TypeError:不是在字符串格式化python期间转换的所有参数

时间:2021-10-08 04:17:14

The program is supposed to take in two names, and if they are the same length it should check if they are the same word. If it's the same word it will print "The names are the same". If they are the same length but with different letters it will print "The names are different but the same length". The part I'm having a problem with is in the bottom 4 lines.

程序应该包含两个名称,如果它们是相同的长度,则应该检查它们是否相同。如果是同一个单词,就会打印“名字是一样的”。如果它们的长度相同,但是用不同的字母,就会打印出“名字不同但长度相同”。我有问题的部分在下面4行。

#!/usr/bin/env python
# Enter your code for "What's In (The Length Of) A Name?" here.
name1 = input("Enter name 1: ")
name2 = input("Enter name 2: ")
len(name1)
len(name2)
if len(name1) == len(name2):
    if name1 == name2:
        print ("The names are the same")
    else:
        print ("The names are different, but are the same length")
    if len(name1) > len(name2):
        print ("'{0}' is longer than '{1}'"% name1, name2)
    elif len(name1) < len(name2):
        print ("'{0}'is longer than '{1}'"% name2, name1)

When I run this code it displays:

当我运行这段代码时,它会显示:

Traceback (most recent call last):
  File "program.py", line 13, in <module>
    print ("'{0}' is longer than '{1}'"% name1, name2)
TypeError: not all arguments converted during string formatting

Any suggestions are highly appreciated.

非常感谢您的建议。

6 个解决方案

#1


145  

You're mixing different format functions.

你混合了不同的格式函数。

The old-style % formatting uses % codes for formatting:

老式的%格式使用%代码进行格式化:

'It will cost $%d dollars.' % 95

The new-style {} formatting uses {} codes and the .format method

新风格的{}格式使用{}代码和.format方法

'It will cost ${0} dollars.'.format(95)

Note that with old-style formatting, you have to specify multiple arguments using a tuple:

注意,对于旧式格式,您必须使用tuple指定多个参数:

'%d days and %d nights' % (40, 40)

In your case, since you're using {} format specifiers, use .format:

在您的示例中,由于您使用的是{}格式说明符,请使用.format:

"'{0}' is longer than '{1}'".format(name1, name2)

#2


36  

The error is in your string formatting.

错误在字符串格式中。

The correct way to use traditional string formatting using the '%' operator is to use a printf-style format string (Python documentation for this here: http://docs.python.org/2/library/string.html#format-string-syntax):

使用“%”操作符使用传统字符串格式的正确方法是使用printf格式的字符串(这里的Python文档是:http://docs.python.org/2/library/str.html #format-string-syntax):

"'%s' is longer than '%s'" % (name1, name2)

However, the '%' operator will probably be deprecated in the future. The new PEP 3101 way of doing things is like this:

然而,“%”操作符将来可能会被弃用。新的PEP 3101方法是这样的:

"'{0}' is longer than '{1}'".format(name1, name2)

#3


24  

For me, This error was caused when I was attempting to pass in a tuple into the string format method.

对于我来说,这个错误是在我试图将一个元组传递到string format方法时造成的。

I found the solution from this question/answer

我从这个问题/答案中找到了答案

Copying and pasting the correct answer from the link (NOT MY WORK):

复制并粘贴正确答案的链接(不是我的作品):

>>> thetuple = (1, 2, 3)
>>> print "this is a tuple: %s" % (thetuple,)
this is a tuple: (1, 2, 3)

Making a singleton tuple with the tuple of interest as the only item, i.e. the (thetuple,) part, is the key bit here.

将单例元组作为惟一项,即(thetuple)部分,是这里的关键部分。

#4


3  

In addition to the other two answers, I think the indentations are also incorrect in the last two conditions. The conditions are that one name is longer than the other and they need to start with 'elif' and with no indentations. If you put it within the first condition (by giving it four indentations from the margin), it ends up being contradictory because the lengths of the names cannot be equal and different at the same time.

除了另外两个答案,我认为在最后两个条件下缩进也是不正确的。条件是一个名字比另一个名字长,他们需要以“elif”开头,没有缩进。如果你把它放在第一个条件中(通过给它从页边距上的四个缩进),它最终是矛盾的,因为名称的长度不能同时相等和不同。

    else:
        print ("The names are different, but are the same length")
elif len(name1) > len(name2):
    print ("{0} is longer than {1}".format(name1, name2))

#5


3  

In my case, it's because I need only a single %s, i missing values input.

在我的例子中,因为我只需要一个%s,所以我丢失了值输入。

#6


0  

I encounter the error as well,

我也遇到了错误,

_mysql_exceptions.ProgrammingError: not all arguments converted during string formatting 

But list args work well.

但是清单很好用。

I use mysqlclient python lib. The lib looks like not to accept tuple args. To pass list args like ['arg1', 'arg2'] will work.

我使用mysqlclient python库。这个库看起来不接受元组args。要通过像['arg1'、'arg2']这样的列表args将有效。

#1


145  

You're mixing different format functions.

你混合了不同的格式函数。

The old-style % formatting uses % codes for formatting:

老式的%格式使用%代码进行格式化:

'It will cost $%d dollars.' % 95

The new-style {} formatting uses {} codes and the .format method

新风格的{}格式使用{}代码和.format方法

'It will cost ${0} dollars.'.format(95)

Note that with old-style formatting, you have to specify multiple arguments using a tuple:

注意,对于旧式格式,您必须使用tuple指定多个参数:

'%d days and %d nights' % (40, 40)

In your case, since you're using {} format specifiers, use .format:

在您的示例中,由于您使用的是{}格式说明符,请使用.format:

"'{0}' is longer than '{1}'".format(name1, name2)

#2


36  

The error is in your string formatting.

错误在字符串格式中。

The correct way to use traditional string formatting using the '%' operator is to use a printf-style format string (Python documentation for this here: http://docs.python.org/2/library/string.html#format-string-syntax):

使用“%”操作符使用传统字符串格式的正确方法是使用printf格式的字符串(这里的Python文档是:http://docs.python.org/2/library/str.html #format-string-syntax):

"'%s' is longer than '%s'" % (name1, name2)

However, the '%' operator will probably be deprecated in the future. The new PEP 3101 way of doing things is like this:

然而,“%”操作符将来可能会被弃用。新的PEP 3101方法是这样的:

"'{0}' is longer than '{1}'".format(name1, name2)

#3


24  

For me, This error was caused when I was attempting to pass in a tuple into the string format method.

对于我来说,这个错误是在我试图将一个元组传递到string format方法时造成的。

I found the solution from this question/answer

我从这个问题/答案中找到了答案

Copying and pasting the correct answer from the link (NOT MY WORK):

复制并粘贴正确答案的链接(不是我的作品):

>>> thetuple = (1, 2, 3)
>>> print "this is a tuple: %s" % (thetuple,)
this is a tuple: (1, 2, 3)

Making a singleton tuple with the tuple of interest as the only item, i.e. the (thetuple,) part, is the key bit here.

将单例元组作为惟一项,即(thetuple)部分,是这里的关键部分。

#4


3  

In addition to the other two answers, I think the indentations are also incorrect in the last two conditions. The conditions are that one name is longer than the other and they need to start with 'elif' and with no indentations. If you put it within the first condition (by giving it four indentations from the margin), it ends up being contradictory because the lengths of the names cannot be equal and different at the same time.

除了另外两个答案,我认为在最后两个条件下缩进也是不正确的。条件是一个名字比另一个名字长,他们需要以“elif”开头,没有缩进。如果你把它放在第一个条件中(通过给它从页边距上的四个缩进),它最终是矛盾的,因为名称的长度不能同时相等和不同。

    else:
        print ("The names are different, but are the same length")
elif len(name1) > len(name2):
    print ("{0} is longer than {1}".format(name1, name2))

#5


3  

In my case, it's because I need only a single %s, i missing values input.

在我的例子中,因为我只需要一个%s,所以我丢失了值输入。

#6


0  

I encounter the error as well,

我也遇到了错误,

_mysql_exceptions.ProgrammingError: not all arguments converted during string formatting 

But list args work well.

但是清单很好用。

I use mysqlclient python lib. The lib looks like not to accept tuple args. To pass list args like ['arg1', 'arg2'] will work.

我使用mysqlclient python库。这个库看起来不接受元组args。要通过像['arg1'、'arg2']这样的列表args将有效。