Numbers passed as command line arguments in python not interpreted as integers

前端 未结 6 1091
有刺的猬
有刺的猬 2020-12-10 10:42

I am familiar with C, and have started experimenting in python. My question is regarding the sys.argv command. I\'ve read it is used for a command line interpre

6条回答
  •  半阙折子戏
    2020-12-10 10:47

    You can convert the arguments to integers using int()

    import sys
    
    a = int(sys.argv[1])  b = int(sys.argv[2])
    
    print a, b
    
    print a+b
    

    input: python mySum.py 100 200

    output:

    100 200
    300
    

提交回复
热议问题