出于某种原因,我不能在列表中保存不止一件事(当它在 for 循环中时)
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))
正如你所看到的,我想将动漫列表中的项目[0]保存到mood_list,但只保存具有特定情绪类型的人的名字...出于某种原因我不能找到一种方法来保存 2 个动漫名称。我将动画的名称保存在 k 中,然后将其附加到 mood_list 但最后的 mood_list 仅包含具有特定心情的最后一部动画(它不能保存超过 2 个动画心情一样)请帮忙
在您的 for 循环中,每次 运行 mood_list=list()
时,您都是 re-defining 列表。相反,你会想做这样的事情:
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)
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))
正如你所看到的,我想将动漫列表中的项目[0]保存到mood_list,但只保存具有特定情绪类型的人的名字...出于某种原因我不能找到一种方法来保存 2 个动漫名称。我将动画的名称保存在 k 中,然后将其附加到 mood_list 但最后的 mood_list 仅包含具有特定心情的最后一部动画(它不能保存超过 2 个动画心情一样)请帮忙
在您的 for 循环中,每次 运行 mood_list=list()
时,您都是 re-defining 列表。相反,你会想做这样的事情:
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)