“语法错误:无效语法"尝试在命令提示符中从 IDLE 运行 .py 脚本时

编程入门 行业动态 更新时间:2024-10-26 12:28:51
本文介绍了“语法错误:无效语法"尝试在命令提示符中从 IDLE 运行 .py 脚本时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我刚开始学习python.我使用 IDLE 编辑器创建了一个简单的 .py 文件,并尝试从命令提示符运行它.但是,每次它不断给我语法错误:无效语法"留言.

I just started learning python. I created a simple .py file using the IDLE editor and I am trying to run it from the command prompt. However, every time it keeps giving me the "SyntaxError: Invalid Syntax" message.

这是使用记事本打开 .py 文件时的外观:

This is how the .py file looks when opened with notepad:

 Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32

 Type "copyright", "credits" or "license()" for more information.

 >>> import sys

 >>> print(sys.platform)

 win32

 >>> x="Spam!"

 >>> print(x*8)

 Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!

 >>> print(2**100)

 1267650600228229401496703205376

 >>> 

这就是我在 cmd 中输入的内容:

And this is what I type in the cmd:

 C:\code\script1.py

我们将不胜感激.

推荐答案

那不是 Python 程序,而是交互式(命令提示符)会话的日志.

That's not a Python program, it's the log of an interactive (command prompt) session.

相反,尝试在任何文本编辑器(例如记事本、记事本++)中输入以下内容,将其另存为 C:\code\script2.py,然后像您一样运行:

Instead, try entering the following in any text editor (e.g. notepad, notepad++), save it as C:\code\script2.py and then run it as you did:

import sys

print(sys.platform)

x="Spam!"
print(x*8)

print(2**100)

如果您想为此使用Idle,请单击[文件][新建]创建一个Python源代码文件,输入上面的内容,保存,然后像您一样运行.

If you want to use Idle for this, click [File][New] to create a Python source code file, type in the above, save it and then run it as you did.

Idle 是交互式开发环境 (IDE) 的一个示例.由于您是编程新手:IDE 往往会掩盖正在发生的事情,尽管 Idle 并不是一个严重的情况.因此,使用单独的编辑器并像您一样从命令行运行实际上是熟悉幕后情况的好方法.从长远来看,这将在很多方面获得回报.

Idle is and example of an Interactive Development Environment (IDE). Since you're new to programming: IDE's tend to obscure what's going on, although Idle isn't a severe case of this. So using a separate editor and running from the command line as you did is actually a good way to familiarize yourself with what's going on under the hood. This will pay off in many ways in the long run.

这篇关于“语法错误:无效语法"尝试在命令提示符中从 IDLE 运行 .py 脚本时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 12:59:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409438.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   脚本   命令提示符   语法错误   py

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!