使用print()函数时语法无效

发布于 2025-02-05 03:53:14 字数 793 浏览 2 评论 0原文

编辑:我想出了如何添加屏幕截图

我正在学习python一堂课),并下载了一个python壳以在我的PC上练习。其中一个示例是使用“打开”和“命令”。当我尝试在程序中运行它时,它说“打印”命令上存在无效的语法错误。我进入了内置的帮助程序,以试图弄清楚出了什么问题,一个多小时后我找不到。我尝试使用程序教程中的示例(使用副本和粘贴),并出现了同样的事情。

这是帮助程序中的代码:

def f(a, L=[]):
    L.append(a)
    return L
print(f(1))
print(f(2))
print(f(3))
SyntaxError: invalid syntax (highlights the first p in "print(f(1))"


>>> def f(a, L=[]):
...     L.append(a)
...     return L
... 
... print(f(1))
... print(f(2))
... print(f(3))

它在“ print(f(1))”行中的“ p”之前提出了无效的语法。

我在这里看了一个小时的答案,找不到答案。在t上的“打印”之后,有很多人在p上,当python从2到3到3时,显然与“打印”命令有很多混乱,但“打印”之前什么都没有。

(如果我能弄清楚如何在PC上进行屏幕截图,我会详细显示,但是每次尝试弄乱时),

谢谢您的任何帮助!

Edit: I figured out how to add the screenshot

I'm learning Python (literally learning, in a class), and downloaded a Python shell to practice on my PC. One of the examples is using the "open" and "with" commands. When I tried to run it in my program, it says there is a invalid syntax error on the "print" command. I went into the built in Help programs to try to figure out what was wrong and I couldn't find it after over an hour. I tried using the examples in the tutorial of the program (using copy and paste), and it came out with the same thing.

Here's the code from the Help program:

def f(a, L=[]):
    L.append(a)
    return L
print(f(1))
print(f(2))
print(f(3))
SyntaxError: invalid syntax (highlights the first p in "print(f(1))"


>>> def f(a, L=[]):
...     L.append(a)
...     return L
... 
... print(f(1))
... print(f(2))
... print(f(3))

It comes up with a Invalid Syntax before the "p" in the "print(f(1))" line.

I've looked on here for close to an hour for an answer and can't find the answer. There were plenty of them AFTER "print" focused on the t, apparently there was a lot of confusion with the "print" command when Python went from 2 to 3, but nothing BEFORE the "print".

(If I could figure out how to do a screenshot on my PC, I'd show it in detail, but every time I try it messes up)

Thank you for any help!

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

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

发布评论

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

评论(2

过期以后 2025-02-12 03:53:15

因此,当我遇到错误时,我所做的就是打开文件。在寻求帮助时,我尝试使用我在帮助文档中发现的代码。那也没有用。

我没有关闭最初打开的文件,这在以后的代码中引起了错误。

这是初学者的提示,请确保您的文件在启动新的一系列代码之前关闭,甚至关闭并重新打开Python编辑器以确保。

感谢Nick,Kelly Bundy和Samwise的所有帮助和耐心。如果我能弄清楚如何为您投票,我会的。

So, what I did was to open a file, when I came across an error. In looking for help, I tried to use code I found in the Help documentation. That didn't work either.

I didn't close the file I originally opened, and that caused the errors in the later code.

Here's a tip to the beginners, make sure your file is closed before you start a new series of code, even close and reopen the Python editor to make sure.

Thank you to Nick, Kelly Bundy, and Samwise for all the help and patience. If I could figure out how to upvote you, I would.

嘿哥们儿 2025-02-12 03:53:15

我认为这可以正常工作,也许问题在口译员或Python安装本身中

I think this works properly, maybe the problem is in the interpreter or python installation itself
Google colab

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文