python - Why am I getting an index out of range in one place but not the other? -


def extendedstring(string1, string2):     newstring = ""     if len(string1) == len(string2):         in range(0, len(string1)):             newstring = newstring + string1[i] + string2[i]         return newstring     else:         if len(string1) < len(string2):             in range(0, len(string2)):                 string1 = string1 + string1[i - 1]             in range(0, len(string1)):                 newstring = newstring + string1[i] + string2[i]             return newstring         else:             in range(0, len(string1)):                 string2 = string2 + string2[i - 1]             in range(0, len(string2)):                 newstring = newstring + string1[i] + string2[i]             return newstring 

within first if statement use code:

            newstring = newstring + string1[i] + string2[i] 

and no index out of bounds error in else statement use exact same line of code on 12th line , index out of range error, why this?

it may because in if statement above, 2 strings, "string1" , "string2" have same length , hence can reference equal amount of elements, i.

but in error area, if statement states len(string1) < len(string2) , hence, can not have same size in end.

they don't have same in string1[i] , string2[i] if 1 larger.

in line 9, instead of:

for in range(0, len(string2)): 

use:

for in range(0, len(string1)): 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -