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)
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 ->
list is your last index.