简体   繁体   English

Python — TypeError: format() 最多接受 2 个参数(给出 3 个)

[英]Python — TypeError: format() takes at most 2 arguments (3 given)

I'm taking an intro course to Python3 by SoloLearn.我正在参加 SoloLearn 的 Python3 入门课程。 This is a code example they give, but when I run it in Python3 or in Python2, I get similar errors.这是他们给出的代码示例,但是当我在 Python3 或 Python2 中运行它时,我得到了类似的错误。 Here's the code, on introduction to String Formatting:这是介绍字符串格式的代码:

nums = [4, 5, 6]
msg = "Numbers: {0} {1} {2}".
format(nums[0], nums[1], nums[2])
print(msg)

which is supposed to result in:这应该导致:

>>>
Numbers: 4 5 6
>>>

But I get a Syntax Error on line 2, which points to the "."但是我在第 2 行收到一个语法错误,它指向“.”。 at the end of the line as being an invalid use of syntax.在行尾作为语法的无效使用。

And I get the TypeError: format() takes at most 2 arguments (3 given) when I try to execute line 3.当我尝试执行第 3 行时,我得到TypeError: format() 最多需要 2 个参数(给定3 个)。

Why!?为什么!?

Lines 2 and 3 should be one line:第 2 行和第 3 行应该是一行:

msg = "Numbers: {0} {1} {2}".format(nums[0], nums[1], nums[2])

If the code had that line break in the actual course, they need to get their act together.如果代码在实际课程中有那个换行符,他们需要一起行动。 If you introduced the line break, don't do that.如果您引入了换行符,请不要这样做。

If you want to put the format in another line, you either have to put a backslash like this:如果要将format放在另一行中,则必须像这样放置反斜杠:

msg = "Numbers: {0} {1} {2}".\
format(nums[0], nums[1], nums[2])

or wrap it with parenthesis, or better yet, put the format in the same line或者用括号括起来,或者更好的是,将format放在同一行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python TypeError:int() 最多接受 2 个参数(给出 3 个) - Python TypeError: int() takes at most 2 arguments (3 given) TypeError:write()最多接受5个参数(给定6个参数)Python Excel - TypeError: write() takes at most 5 arguments (6 given) Python Excel Python 数据集更新 - TypeError: update() 最多需要 2 个位置参数(给出 3 个) - Python dataset update - TypeError: update() takes at most 2 positional arguments (3 given) Python错误“ TypeError:sort()最多接受2个参数(给定3个)” - Python error “TypeError: sort() takes at most 2 arguments (3 given)” Python请求-TypeError:put()最多接受2个参数(给定4个) - Python requests - TypeError: put() takes at most 2 arguments (4 given) Python-子类TypeError:最多接受2个参数(给定3个),而应该接受3个 - Python - Subclass TypeError: takes at most 2 arguments (3 given) while it should take 3 TypeError:file()最多需要3个参数(给定4个参数) - TypeError: file() takes at most 3 arguments (4 given) TypeError:execute()最多接受3个参数(给定11个) - TypeError: execute() takes at most 3 arguments (11 given) 类型错误:UMat() 最多接受 2 个参数(给出 3 个) - TypeError: UMat() takes at most 2 arguments (3 given) TypeError: function takes at most 2 arguments (4 given) - TypeError: function takes at most 2 arguments (4 given)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM