r/pythonhelp • u/wise_man_of_the_hill • Jan 23 '24
Trouble with indexing
I'm having trouble with a small program I'm doing for a class. When I run it, it gives me an error with line 25 (in bold below), saying 'IndexError: list index out of range'. No matter what I set the stops for the ranges to, it gives me the same error. I'm new to posting here, so I may have made a few mistakes.
def main():
bigLoop=0
smallLoop=0
total=0
creatorFavorites=\["Purple", "Crimson", "Gold", "Teal", "Bronze"\]
userFavorites=\[\]
similar=\[\]
userFavorite0=(str(input("What is your favorite color?")))
userFavorites.append(userFavorite0)
userFavorite1=(str(input("Second favorite?")))
userFavorites.append(userFavorite1)
userFavorite2=(str(input("Third favorite?")))
userFavorites.append(userFavorite2)
userFavorite3=(str(input("Fourth favorite?")))
userFavorites.append(userFavorite3)
userFavorite4=(str(input("Fifth favorite?")))
userFavorites.append(userFavorite4)
for a in range(5):
for b in range(5):
sadlyNecessary=smallLoop-1
**if creatorFavorites\[int(bigLoop)\]==userFavorites\[int(smallLoop)\]:**
similar.append(creatorFavorites\[sadlyNecessary\])
total=total+1
smallLoop=int(smallLoop)+1
bigLoop=int(bigLoop)+1
print("The colors in both of our lists were "+str(similar)+" for a total of "+str(total)+".")main()
1
Upvotes
View all comments
Show parent comment
2
u/wise_man_of_the_hill Jan 24 '24
I printed the values like you said and found my problem. I never reset smallLoop, so it kept adding and adding when bigLoop going. Thank you so much, I spent over a day trying to fix this.