我想更新列表以包含用户添加的新项目。有一些条件,例如代码必须是7位数长。如果代码已存在,系统将通知用户。如果用户尝试使用不同的代码添加另一个“up”副本,系统将不允许它。这将使他们再次尝试,因为代码必须相同。最后我会包含一个视频编号,所以如果有两个'up'副本,他们会有两个不同的视频编号,但视频编码相同。
有人可以告诉我为什么以下代码对我不起作用?
all_movies = []
class Movie(object):
movie_list = []
def __init__(self, code, title, director):
self.code = code
self.title = title
self.director = director
Movie.movie_list.append(self)
@staticmethod
def add_movie():
mv_code = input("Code of movie: ")
movie_code = int(mv_code)
movie_title = input("Name of movie: ")
movie_director = input("Director: ")
if len(mv_code) == 7:
all_movies.append(Movie(movie_code, movie_title, movie_director))
print("movie added to database")
else:
print("the code must be 7 digits long, add movie again.")
def check_validity(movie_code, all_movies):
if movie_code in all_movies:
return True
else:
return False
if check_validity(movie_code, all_movies):
all_movies[all_movies] += Movie
print()
print("updated")
else:
all_movies[movie_code] = [movie_code, movie_title, movie_director]
def main():
movie1 = Movie(1122334, 'Up', 'Director')
movie2 = Movie(1231235, 'Taxi Driver', 'Film-maker')
这是我收到的错误消息:
all_movies[movie_code] = [movie_code, movie_title, movie_director]
IndexError: list assignment index out of range
答案 0 :(得分:0)
首先,这种结构不适合您的需求。
关于你得到的错误,我应该说你必须将所有电影定义为字典而不是列表(因为你想要为每部电影使用)。
试试这个:
all_movies = {}
在你add_movie
方法中:
@staticmethod
def add_movie():
mv_code = input("Code of movie: ")
movie_code = int(mv_code)
movie_title = input("Name of movie: ")
movie_director = input("Director: ")
if len(mv_code) == 7:
if movie_code in all_movies.keys():
print("the movie already exists")
# what do you want to happen here ?
else:
all_movies[movie_code] = (movie_code, movie_title, movie_director)
print("movie added to database")
else:
print("the code must be 7 digits long, add movie again.")
这会将电影添加到all_movies
,您不需要其他代码,我也不了解init
和movie_list
的使用情况。
试试这个,然后告诉我如果电影中已存在代码会发生什么,我会为你更新我的答案。
根据您的评论意愿,该方法将更新为以下内容:
@staticmethod
def add_movie():
mv_code = input("Code of movie: ")
movie_code = int(mv_code)
movie_title = input("Name of movie: ")
movie_director = input("Director: ")
if len(mv_code) == 7:
if movie_code in all_movies.keys():
print("the movie is already exists, adding it with another video number")
# all_movies[movie_code][-1] is the last video with an existing key
# all_movies[movie_code][-1][-1] last video number generated
new_video_number = all_movies[movie_code][-1][-1] + 1
all_movies[movie_code].append([movie_title, movie_director, new_video_number]) # adding it with new video number
print("movie added to database with new video number")
else:
all_movies[movie_code] = []
all_movies[movie_code].append([movie_title, movie_director, 1]) # 1 is the first movie added(video number)
print("movie added to database")
else:
print("the code must be 7 digits long, add movie again.")
这将返回all_movies
,如下所示:
{
'1231235':[
['Taxi Driver', 'Film-maker', 1]
]
'1122334':[
['Up', 'Director',1],
['Up', 'Director',2],
['Up', 'Director',3],
]
'1122333':[
['Another movie', 'Another Director',1],
['Another movie', 'Another Director',2],
]
}
内部列表的最后一个元素是系统自动生成的video_numbers
。