Python 3.3:分离参数(sep)给出错误 [英] Python 3.3: separation argument (sep) giving an error

查看:170
本文介绍了Python 3.3:分离参数(sep)给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程很陌生,我是从 Python 开始的.我试图在这里查找我的问题,但没有真正找到任何东西.

I am very new to programming and I'm starting out with Python. I tried to look up my question here but didn't really find anything.

我正在尝试执行一个非常简单的打印命令,但由于某种我不明白的原因出现错误.

I'm trying to work a very simple print command but I'm getting an error for some reason that I don't understand.

last = 'smith'
middle = 'paul'
first = 'john'
print(first.capitalize(), middle.capitalize(), last.capitalize(), sep='\t')

根据书中的答案,这应该是正确的,但是每次我尝试运行它时,都会收到'sep'的错误:

According to the answer in the book, this should be right, but every time I try to run it, I get an error with the 'sep':

print(first.capitalize(), middle.capitalize(), last.capitalize(), sep='\t')
                                                                     ^
SyntaxError: invalid syntax

谁能告诉我我做错了什么.我正在使用 PyScripter.

Can someone tell me what I'm doing wrong. for what it's worth I'm using PyScripter.

谢谢你.我发现我使用的是 Python 2.7.3 而不是 3.3.所以我查阅了手册以了解分隔符是如何工作的.在我看来,唯一的区别在于方括号.手册将打印功能描述为:

Thanks for that. I found out that I'm using Python 2.7.3 instead of 3.3. So I looked up the manual to see how the separator works. It seems to me that the only difference is with the square bracket. The manual describes the print function as :

print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout]) 

所以我改变了我的打印命令并添加了方括号:

So I changed my print command and added the square bracket:

print ([first.capitalize(),middle.capitalize(),last.capitalize()] [, sep='\t'])

但不幸的是,这也不起作用,因为我收到一个错误,突出显示了 sep='\t' 周围的方括号.即使我去掉括号,错误也不会消失.

but unfortunately this doesn't work either as I get an error that highlights the square brackets around sep='\t'. Even when I take the brackets out, the error doesn't go away.

我不确定我做错了什么,看起来应该很简单.

I'm not sure what I'm doing wrong, it seems like it should be very simple.

推荐答案

您实际上并没有使用 Python 3,您只是认为您在使用.试试:

You aren't actually using Python 3, you just think you are. Try:

import sys
print(sys.version)

然后看看结果.Python 2 print ... 语句(不是 Python 3 中的 print(...) 函数)将其解释为

and see what comes out. The Python 2 print ... statement (not print(...) function in Python 3) interprets this as

print (first.capitalize(), middle.capitalize(), last.capitalize(), sep='\t')

试图用关键字参数打印元组,因此 sep

which is trying to print a tuple with a keyword argument, thus the syntax error on sep

这篇关于Python 3.3:分离参数(sep)给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆