Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading