我正试图将用户响应存储到一个列表中,并将其插入到每个键(名称(中,我不知道如何将每个列表与其对应的键分开。我知道,通过将每个响应附加到列表(电影(中,它最多可以添加10个单独的元素。如果我要做一个猜测,我必须重复提出每个问题的过程,并将答案存储在一个新的列表中?
favorite_movies = {}
movies = []
active = True
while active:
name = input("What is your name? ")
one = input("What are your top 5 favorite movies? ")
movies.append(one)
two = input("Movie #2- ")
movies.append(two)
three = input("Movie #3- ")
movies.append(three)
four = input("Movie #4- ")
movies.append(four)
five = input("Movie #5- ")
movies.append(five)
favorite_movies[name] = movies
repeat = input("Enter 'quit' when finished, or 'next' to move on to the next person. ")
if repeat == 'quit':
active = False
else:
continue
通过在循环中移动电影列表,每次运行循环时都会初始化一个新列表
favorite_movies = {}
active = True
while active:
movies = [] # move movies list inside the loop
name = input("What is your name? ")
one = input("What are your top 5 favorite movies? ")
movies.append(one)
two = input("Movie #2- ")
movies.append(two)
three = input("Movie #3- ")
movies.append(three)
four = input("Movie #4- ")
movies.append(four)
five = input("Movie #5- ")
movies.append(five)
favorite_movies[name] = movies
repeat = input("Enter 'quit' when finished, or 'next' to move on to the next person. ")
if repeat == 'quit':
active = False
else:
continue
将movies = []
移动到循环中,以便为每个循环创建一个新列表。否则,你会一次又一次地附加到同一个。