TypeError: slice indices must be integers or None or have an __index__ method in Python

Question:

When I run the code I get the error TypeError: slice indices must be integers or None or have an __index__ method and it tells me where it is in the code, just not sure how to fix it and get rid of the type error. It says the error is in line 244

line 244, in <module>
    urls[page_number * 100 : (page_number * 100) + 100],
TypeError: slice indices must be integers or None or have an __index__ method

Here is the code

st.write(len(urls))
page_number = st.number_input("Page number", 0, 100)
urls = list(
    map(
        lambda x: {
            "src": cdn_url + x + "_output.png",
            "width": 512,
            "height": 512,
            "id": x,
        },
        urls[page_number * 100 : (page_number * 100) + 100],
    )
)
select = stgrid(urls, zoom=zoom, height=2000)

Asked By: orangey

||

Answers:

This code sample reproduces the problem you have:

urls = list(range(100000)) # just making an arbitrary list
page_number = 3.1416
urls[page_number * 100 : (page_number * 100) + 100]

Using an integer instead, we can prevent this error:

page_number = 3
urls[page_number * 100 : (page_number * 100) + 100]

So to solve this: You can either round or cast an int for the page_number, or raise your own exception if an integer is not entered for the page_number.

Answered By: JacobIRR