Argv - String into Integer

后端 未结 3 1531
一生所求
一生所求 2020-12-10 13:35

I\'m pretty new at python and I\'ve been playing with argv. I wrote this simple program here and getting an error that says :

TypeError: %d format: a

3条回答
  •  悲&欢浪女
    2020-12-10 14:19

    Assign the converted integers to those variables:

    num1 = int(argv[1])  #assign the return int to num1
    num2 = int(argv[2])
    

    Doing just:

    int(argv[1])
    int(argv[2])
    

    won't affect the original items as int returns a new int object, the items inside sys.argv are not affected by that.

    Yo modify the original list you can do this:

    argv[1:] = [int(x) for x in argv[1:]]
    file_name, num1, num2 = argv  #now num1 and num2 are going to be integers
    

提交回复
热议问题