This is my python hello.py
script:
这是我的python hello。py脚本:
def hello(a,b):
print "hello and that's your sum:"
sum=a+b
print sum
import sys
if __name__ == "__main__":
hello(sys.argv[2])
The problem is that it can't be run from the windows command line prompt, I used this command:
问题是它不能从windows命令行提示符运行,我使用了这个命令:
C:\Python27>hello 1 1
But it didn't work unfortunately, may somebody please help?
但不幸的是,有人能帮忙吗?
5 个解决方案
#1
16
-
import sys
out of hello function. - 从hello函数导入sys。
- arguments should be converted to int.
- 参数应该转换为int。
- String literal that contain
'
should be escaped or should be surrouned by"
. - 包含“应该被转义或应该被包围”的字符串文字。
- Did you invoke the program with
python hello.py <some-number> <some-number>
in command line? -
您是否用python hello调用了这个程序?在命令行中,py
< somenumber > ?
import sys
def hello(a,b):
print "hello and that's your sum:", a + b
if __name__ == "__main__":
a = int(sys.argv[1])
b = int(sys.argv[2])
hello(a, b)
#2
7
To execute your program from the command line, you have to call the python interpreter, like this :
要从命令行执行程序,必须调用python解释器,如下所示:
C:\Python27>python hello.py 1 1
If you code resides in another directory, you will have to set the python binary path in your PATH environment variable, to be able to run it, too. You can find detailed instructions here.
如果代码驻留在另一个目录中,则必须在path环境变量中设置python二进制路径,以便能够运行它。你可以在这里找到详细的说明。
#3
2
Your indentation is broken. This should fix it:
你的缩进是坏了。这应该改正:
import sys
def hello(a,b):
print 'hello and thats your sum:'
sum=a+b
print sum
if __name__ == "__main__":
hello(sys.argv[1], sys.argv[2])
Obviously, if you put the if __name__
statement inside the function, it will only ever be evaluated if you run that function. The problem is: the point of said statement is to run the function in the first place.
显然,如果将if __name__语句放入函数中,只有在运行该函数时才会对其进行评估。问题是:声明的要点是首先运行函数。
#4
2
Here are all of the previous answers summarized:
以下是之前的所有回答:
- modules should be imported outside of functions.
- 模块应该在函数之外导入。
- hello(sys.argv[2]) needs to be indented since it is inside an if statement.
- 您好(sys.argv[2])需要缩进,因为它在if语句中。
- hello has 2 arguments so you need to call 2 arguments.
- hello有两个参数,所以需要调用两个参数。
- as far as calling the function from terminal, you need to call python .py ...
- 只要调用终端的函数,就需要调用python .py…
The code should look like this:
代码应该是这样的:
import sys
def hello(a, b):
print "hello and that's your sum:"
sum = a+b
print sum
if __name__== "__main__":
hello(int(sys.argv[1]), int(sys.argv[2]))
Then run the code with this command:
然后用这个命令运行代码:
python hello.py 1 1
#5
1
import sys
def hello(a, b):
print 'hello and that\'s your sum: {0}'.format(a + b)
if __name__ == '__main__':
hello(int(sys.argv[1]), int(sys.argv[2]))
Moreover see @thibauts answer about how to call python script.
另外,请参阅@thibauts关于如何调用python脚本的答案。
#1
16
-
import sys
out of hello function. - 从hello函数导入sys。
- arguments should be converted to int.
- 参数应该转换为int。
- String literal that contain
'
should be escaped or should be surrouned by"
. - 包含“应该被转义或应该被包围”的字符串文字。
- Did you invoke the program with
python hello.py <some-number> <some-number>
in command line? -
您是否用python hello调用了这个程序?在命令行中,py
< somenumber > ?
import sys
def hello(a,b):
print "hello and that's your sum:", a + b
if __name__ == "__main__":
a = int(sys.argv[1])
b = int(sys.argv[2])
hello(a, b)
#2
7
To execute your program from the command line, you have to call the python interpreter, like this :
要从命令行执行程序,必须调用python解释器,如下所示:
C:\Python27>python hello.py 1 1
If you code resides in another directory, you will have to set the python binary path in your PATH environment variable, to be able to run it, too. You can find detailed instructions here.
如果代码驻留在另一个目录中,则必须在path环境变量中设置python二进制路径,以便能够运行它。你可以在这里找到详细的说明。
#3
2
Your indentation is broken. This should fix it:
你的缩进是坏了。这应该改正:
import sys
def hello(a,b):
print 'hello and thats your sum:'
sum=a+b
print sum
if __name__ == "__main__":
hello(sys.argv[1], sys.argv[2])
Obviously, if you put the if __name__
statement inside the function, it will only ever be evaluated if you run that function. The problem is: the point of said statement is to run the function in the first place.
显然,如果将if __name__语句放入函数中,只有在运行该函数时才会对其进行评估。问题是:声明的要点是首先运行函数。
#4
2
Here are all of the previous answers summarized:
以下是之前的所有回答:
- modules should be imported outside of functions.
- 模块应该在函数之外导入。
- hello(sys.argv[2]) needs to be indented since it is inside an if statement.
- 您好(sys.argv[2])需要缩进,因为它在if语句中。
- hello has 2 arguments so you need to call 2 arguments.
- hello有两个参数,所以需要调用两个参数。
- as far as calling the function from terminal, you need to call python .py ...
- 只要调用终端的函数,就需要调用python .py…
The code should look like this:
代码应该是这样的:
import sys
def hello(a, b):
print "hello and that's your sum:"
sum = a+b
print sum
if __name__== "__main__":
hello(int(sys.argv[1]), int(sys.argv[2]))
Then run the code with this command:
然后用这个命令运行代码:
python hello.py 1 1
#5
1
import sys
def hello(a, b):
print 'hello and that\'s your sum: {0}'.format(a + b)
if __name__ == '__main__':
hello(int(sys.argv[1]), int(sys.argv[2]))
Moreover see @thibauts answer about how to call python script.
另外,请参阅@thibauts关于如何调用python脚本的答案。