samedi 28 mars 2020

List index out of range and i got error in my if block which is inside the while loop

def merge(a1,a2):
    if len(a1)<1:
        return a2
    if len(a2)<1:
        return a1
    a1item=a1[0]
    a2item=a2[0]
    i=1
    j=1
    merge=[]
    while(a1item or a2item):
        print(a1item,a2item)
        if a1item<a2item:
            merge.append(a1item)
            a1item=a1[i]
            i+=1
        else:
            merge.append(a2item)
            a2item=a2[j]
            j+=1
        print(merge)


merge([1,2,3],[3,54,100])

I got error in a1item=a1[i] how to stop when the loop is pointing to last element. Suggest me without using inbuilt function

Aucun commentaire:

Enregistrer un commentaire