Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading