I’m still new to coding and I came across this example. How come the range is 5 and not 6?
NameList=["Olivia", "Madison", "Mia", "Emily", "Charlotte", "Amelia"]
NoMoreSwap=False
while NoMoreSwap==False:
NoMoreSwap=True
for Index in range(5):
if NameList[Index]>NameList[Index+1]:
Temp=NameList[Index]
NameList[Index]=NameList[Index+1]
NameList[Index+1]=Temp
NoMoreSwap=False
print("The name list is: ", NameList)
>Solution :
Since you are working with Index
index and Index + 1
index you would get out of range since last index 5+1 (for Index in range(6))
is the last one in the loop you asked about.
Your list has 6 values -> len(list)==6
but list[5]
is your last index.