[英]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.