TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' in Python

Dung Do Tien Jan 23 2021 322

I'm running my code in Python Ver3.9(Window10) and get the error status: TypeError: int() argument must be a string, a bytes-like object or a number, not 'list', here my code:

#!/usr/bin/python

numbers = ['0123456789']
try:
    print(int(numbers))
    print("++++++++++++++++++")
    print(int(str(numbers)))
    print("//////////////////")
except Exception as e:
    print("==================")
    print(e)

Although I have assigned Type int () to the argument must be a string but I still get the error again, I need a solution to solve it thoroughly on my computer and others too. Help me, many tks guys! 

Have 1 answer(s) found.
  • M

    Milan cubes Jan 22 2021

    Okey, here my solutions: By using:

    int(numbers[0]) or 
    int_list = [int(nums) for nums in numbers] 

    Explain: You can't convert a list to an integer, but you can convert the string value in the list to an integer. so that Here is the complete code you need:

    #!/usr/bin/python
    numbers = ['0123456789']
    try:
        print(int(numbers))
        print("++++++++++++++++++")
        print(int(str(numbers)))
        print("//////////////////")
    except Exception as e:
        print("==================")
        print(e)
        print("eeeeeeeeeeeeeeeeee")
        print(int(numbers[0])) 

    *Note: print(int(numbers[0]))

    I hope my solution useful for you, If you still have questions, please comment below for my support:)))

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close