结尾=》 ”是一个错误

发布于 2024-12-11 17:03:21 字数 268 浏览 0 评论 0原文

可能的重复:
python print end=' '

你好,我一直在自学 python,但遇到了问题end=“”语句。 有人可以帮助我吗?当我在代码中运行它时,它说这是无效的语法。 提前谢谢。我写错了吗?

非常感谢对新手的帮助。

Possible Duplicate:
python print end=' '

Hello I've been teaching myself python, and I'm having trouble with the end= " " statement.
could anybody help me with it?when i run it in my code it said that is is a invalid syntax.
Thx in advance. Am i writing it wrong?

Help for a newbie is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薆情海 2024-12-18 17:03:21

我假设您在使用 print 语句时遇到问题,因此您尝试使用 print(var,end="")。此语法仅对 Python 3.0 有效。如果您使用的是 Python 2.x,那么它会显示错误。为了了解您正在使用哪个版本的 python,请在终端中输入 python --version 。 Python 2.x 和 python 3 在语法上略有不同。您可以在此处阅读它们之间的区别。
顺便说一句,在 Python 2.x 中,您可以通过 print var, 获得相同的结果

I assume that you are having trouble with print statement, So you are trying to use print(var,end=""). This syntax is only valid with Python 3.0. if you are using Python 2.x then it will show you an error. In order to know which version of python you are using type python --version in your terminal. Python 2.x and python 3 slightly differ in syntax. You can Read the the difference between them here.
By the way in Python 2.x you can achieve the same result by print var,

梦年海沫深 2024-12-18 17:03:21

显然不是作业问题:

pax@pax-desktop:~$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> end= " "
>>> len(end)
1
>>> 

因此一定存在另一个问题。由于没有看到错误消息和实际代码,我能提出的唯一建议是:

  • 检查缩进。众所周知,Python 对此非常挑剔,因为它决定代码块是什么。
  • 检查引号是否是常规引号,而不是从 Word 文档或网页剪切粘贴的 “ ” 之类的内容。

当网页创建者懒得发布正确的代码(或者他们的演示引擎消化它)时,最后一个特别烦人,并且在我的系统上,结果是:

pax@pax-desktop:~$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> end= “ ”
  File "<stdin>", line 1
    end= “ ”
         ^
SyntaxError: invalid syntax

Clearly not a problem with an assignment:

pax@pax-desktop:~$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> end= " "
>>> len(end)
1
>>> 

Therefore there must be another issue. Short of seeing the error message and actual code, the only suggestions I can make are:

  • Check indentation. Python is notoriously picky about this since it's how it decides what blocks of code are.
  • Check that the quote marks are the regular ones, not something like “ ” cut'n'pasted from a Word document or web page.

That last one is particularly annoying when web page creators can't be bothered posting proper code (or their presentation engines munge it) and, on my system, results in:

pax@pax-desktop:~$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> end= “ ”
  File "<stdin>", line 1
    end= “ ”
         ^
SyntaxError: invalid syntax
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文