[Solved] index out of range but is in fact in range [closed]


Well try to debug your code by yourself first. Anyhow for your question Why is this happening? :

It gives you error in postCode = split_address[4] because your list has 4 elements 0,1,2,3 and you are accessing the 4th element which is not present..
enter image description here

you don’t have the index[4] thats why it gives you error out of range!

Updated Area:

In your for loop the length of split_address is changing to 4 and 5 and when the length is 4 and your are trying to get index[4] it will give definitely gives you error of index out of range to solve this issue you have to add a check on it.

Try this:

import re
import requests
r = requests.get('https://halalhmc.org/outlets-by-name/')
from bs4 import BeautifulSoup

soup = BeautifulSoup(r.text, 'html.parser')

results = soup.find_all('div', attrs={'class':'outlet-content'})

records = []
for result in results:

    name = result.find('h3').text
    fullAddress = (result.find('p', attrs={'class':'outlet-address'})).text
    split_address = fullAddress.split(',')
    address1 = split_address[0]
    city = split_address[1]
    city1 = split_address[2]
    validPcode = re.match("[A-Z]{2}[0-9] [0-9][A-Z]{2}",split_address[3])
    if validPcode:
        postCode = split_address[3]
    else:
        county = split_address[3]
        if len(split_address) is 4:
            postCode = split_address[3]
        elif len(split_address) is 3:
            postCode = split_address[4]
    records.append((name,address1[10:],city,city1,postCode))

# only for checking
print records[1]
print records[2]
print records[3]
print records[4]
print records[5]

i have modified your code little bit and only adds one check on it(split_address).

Hope you will understand and it will help you! 🙂

3

solved index out of range but is in fact in range [closed]