+-
如何在Windows命令行中使用参数运行Python脚本
这是我的 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])

问题是它无法从Windows命令行提示符运行,我使用了这个命令:

C:\Python27>hello 1 1

但遗憾的是它没有用,有人可以帮忙吗?

最佳答案
>将sys导出hello函数.
>参数应该转换为int.
>包含’应该被转义或应该被包围的字符串文字“.
>您是否使用python hello.py< some-number>调用该程序? <一些数>在命令行?

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)
点击查看更多相关文章

转载注明原文:如何在Windows命令行中使用参数运行Python脚本 - 乐贴网