Home > Mobile >  For some reason i cant save more than one thing on a list(while it is on a for loop)
For some reason i cant save more than one thing on a list(while it is on a for loop)

Time:06-04

from random import choice

# List of anime

anime = [['One Punch Man','Funny','NotBig'],['One piece','NotFunny','Big'],
['MyHeroAc','NotFunny','NotBig']]

#input

mood = input('What anime do you want:')
#print random anime
for item in anime:
    if item[1] == mood:
        k = item[0]
        print(mood   ' anime = '   item [0])
        mood_list = list()
        mood_list.append(k)


print('Do you want a radnom one from the', mood,'anime; Y/N')

ch = input()

if ch == 'Y':
    print(mood_list) #make it pick a random anime of the funny cadigory
    print(len(mood_list))

So as you can see i want to save the item[0] from the anime list to mood_list but only the names of the one that have the specific mood type... for some reason i cant find a way to save 2 of the names of the anime. I save the name of the anime in k and then i append it to the mood_list but it the mood_list at the end holds only the last anime that has the specific mood(it cant save more than 2 anime with the same mood) pls help

CodePudding user response:

In your for loop, you are re-defining the list each time that you run mood_list=list(). Instead, you'll want to do something like this:

mood = input('What anime do you want:')
mood_list = []
#print random anime
for item in anime:
  if item[1] == mood:
    k = item[0]
    print(mood   ' anime = '   item [0])
    mood_list.append(k)
  • Related