ValueError: invalid literal for int() with base 10 in Python

Dung Do Tien Jan 23 2021 158

I'm running my code in Python Ver3.9(Window10) and get the error status: ValueError: invalid literal for int() with base 10, here my code:

 #!/usr/bin/python
import requests
import random

list_host = ['https://google.com/url=',
             'https://youtube.com/url=',
             'https://facebook.com/url=']
host = random.choice(list_host)
print(host)
url = "www.laptop.com"
host_url = host + url
try:
    print(int(list_host[0]))
    connect = requests.get(host_url)
except Exception as e:
    print("==================")
    print(e)
    print("++++++++++++++++++")

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

    majid gholipour Jan 22 2021

    Okay, here my solutions: By using:

     list_host[0] 

    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
    import requests
    import random
    
    list_host = ['https://google.com/url=',
                 'https://youtube.com/url=',
                 'https://facebook.com/url=']
    host = random.choice(list_host)
    print(host)
    url = "www.laptop.com"
    host_url = host + url
    try:
        print(list_host[0])
        connect = requests.get(host_url)
    except Exception as e:
        print("==================")
        print(e)
        print("eeeeeeeeeeeeeeeeee") 

    *Note: print(list_host[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