0
get_code="https://localhost:8080/soieurow/KPP/alksdjfhlkjhekajhf?eowuiro=akleireyi&URL=https%3A%2F%2Flocalhost:8080%2Fmga%2Fsps%2Foauth%2Foauth20%2Fauthorize%3Fresponse_type%3Dcode%26scope%3Dopenid%2Bname%2Bemail%2Bpostal_code%26client_id%3Dthaljlwej%26redirect_uri%3Dhttp%3A%2F%2Faklsdjfhwekdisd.com%3A5006%2Fredirectcode%26nonce%3DQJT8RbymFk%26acrakdjasd%3DD1%26token%3DfkasjfhalskfhlaksjhkL61bqqADtekpH-HE55lZaX2LJH4Ii9diraseufhalksfhl%26correlation_id%3D4102479872341%26support_encryption%3Dsj2aljkadfj3%26state%3Dajk1234"
code='code'
if code in get_code:
    code=get_code.split("code=")[1][:47]
    print("i havce code",code)
else:
    print("i don't find code")

I am passing string, but i am getting IndexError: list index out of range

5
  • 3
    Please no images of code. Commented Aug 21, 2021 at 17:14
  • 3
    you better put the actual code than an image of it, we can't see the string value :) Commented Aug 21, 2021 at 17:16
  • code= does not appear in what you showed of the string you are splitting, in which case there would be no element at index 1. Commented Aug 21, 2021 at 17:20
  • 1
    Look at stackoverflow.com/tour on how to ask questions Commented Aug 21, 2021 at 17:22
  • 1
    share the actual code so we will be able to assist Commented Aug 21, 2021 at 17:23

2 Answers 2

1

The index error is about the first one ([1]) not the second one ([:47]). It's because the get_code string doesn't contain any code= string at all!
So here is what happens:

  • split method returns the list of substrings split by code=; which in turn doesn't find any code= and returns the whole get_code string as an array of length 1. (i.e returns ["https://..."])
  • But you wanted to get the second element of the returned array (as of [1] part) which leads to an error since the array has only one element at the index of 0!
Sign up to request clarification or add additional context in comments.

Comments

1

Your bug is that you asked if 'code' is in the string but you split by 'code='. Fix that and the code will work as expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.