简体   繁体   English

无法通过Windows命令提示符运行简单的Python程序

[英]Cant make simple Python program run through Windows command prompt

So I wrote a very simple python test file called test testProg.py , and it looks like this: 因此,我编写了一个非常简单的python测试文件,称为test testProg.py ,它看起来像这样:

import sys

def adder(a, b):
    sum = a+b
    print sum

if __name__ == "__main__":
    a = int(sys.argv[1])
    b = int(sys.argv[2])
    adder(a, b)

From another question here , I did the command: 这里的另一个问题 ,我执行了命令:

python testProg.py 3 4

However I get the following error message: 但是我收到以下错误消息:

 File "testProg.py", line 5
    print sum
            ^
SyntaxError: invalid syntax

I am honestly not sure what the issue it... I can run python from the command prompt easily with no issue, but why cant I replicate that questions' solution? 老实说,我不确定是什么问题...我可以从命令提示符轻松运行python,没有问题,但是为什么我不能复制该问题的解决方案?

Thanks. 谢谢。

Edit: Python 3.4 is used 编辑:使用 Python 3.4

It looks like you have Python 3 installed. 看起来您已经安装了Python 3。 The code you are running was written for Python 2, which has slightly different syntax. 您正在运行的代码是为Python 2编写的,语法略有不同。 For this example, you need to change that to print(sum) . 对于此示例,您需要将其更改为print(sum) In general, you should search around for information on the difference between Python 2 and 3, and be careful to note what version is used in code you find on the internet. 通常,您应该搜索有关Python 2和3之间差异的信息,并要小心注意在Internet上找到的代码中使用的版本。 Code written for Python 2 will often not run as-is on Python 3. 为Python 2编写的代码通常无法在Python 3上原样运行。

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

相关问题 Python:无法使用Python脚本打开新的命令提示符会话并通过命令行(Windows)运行程序 - Python: Unable to open a new command prompt session and run a program through command line (windows) using Python script 如何从Windows命令提示符运行远程Python程序 - How to run a remote Python program from Windows command prompt 如何在 Windows 7 的命令提示符中运行 Python 程序? - How do I run a Python program in the Command Prompt in Windows 7? 如何使Python 3.9运行命令提示符windows 10? - How to make Python 3.9 run command prompt windows 10? Python,尝试从命令提示符运行程序 - Python, trying to run a program from the command prompt 尝试从命令提示符运行 Python 程序 - Trying to run Python program from command prompt 如何在 Windows 命令提示符下运行 python 文件? - How to run a python file in the windows command prompt? 如何从 windows 命令提示符在虚拟环境中运行 python 程序? - How do I run a python program in a virtual environment from windows command prompt? 在Windows命令提示符下运行Teradata MLOAD命令,但使用Python来触发它 - Run a Teradata MLOAD command in Windows command prompt, but use Python to trigger it Python-在Windows上运行带有提示I / O的简单命令行程序并将其“代理” - Python - Run a simple command-line program with prompted I/O and “proxy” it, on Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM