Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的python字典总是覆盖_Python_Dictionary - Fatal编程技术网

为什么我的python字典总是覆盖

为什么我的python字典总是覆盖,python,dictionary,Python,Dictionary,我想做的是在字典中创建一个字典。它应该是一个电影爱好者俱乐部,你可以在其中向会员帐户添加电影,但当我尝试添加它时,它会被覆盖。 下面是我的代码: import sys movies = {} def option_one(): print('Club Members') print('=' * 12) for name in movies: print(name) application() def option_two(): n

我想做的是在字典中创建一个字典。它应该是一个电影爱好者俱乐部,你可以在其中向会员帐户添加电影,但当我尝试添加它时,它会被覆盖。 下面是我的代码:

import sys

movies = {}


def option_one():
    print('Club Members')
    print('=' * 12)
    for name in movies:
        print(name)
    application()


def option_two():
    name = input('Please enter the user\'s name: ')
    for movie in movies[name]:
        title = movies[name][movie]
        watch = movies[name][movie]['Watched']
        rate = movies[name][movie]['Rating']
        print('Movie', 'Rating', 'Watched', sep=' ' * 5)
        print('=' * 30)

        print(movie, movies[name][movie]['Rating'], movies[name][movie]['Watched'], sep=' ' * 8)

    application()


def option_three():
    name = input('Please enter the member\'s name: ')

    if name in movies:
        movie = input('Please enter the name of the movie: ')
        movies[name][movie]['Watched'] = movies[name][movie]['Watched'] + 1
        for movie in movies[name]:
            if movie not in movies[name][movie]:
                print('Movie not found. ')
            else:
                print('Times watched incremented. ')
    else:
        print('Sorry, member not found. ')
    application()


def option_four():
    name = input('Please enter the member\'s name: ')

    # if the name exists in movies add the movie
    if name in movies:
        # enter information and update dictionary
        movie_title = input('Enter movie name: ')
        times_watched = int(input('Enter times watched: '))
        rating = input('Enter rating: ')
        add_movie = {name: {movie_title: {'Watched': times_watched,
                                          'Rating': rating}}}
        movies.update(add_movie)
        print('Movie added')

    # if name not in movies print member not found call option 4 again
    else:
        print('Member not found')
        option_four()
    application()


def option_five():
    name = input('Enter new member name: ')
    nameDict = {name: ''}

    # update the movies dictionary with name dictionary as key
    movies.update(nameDict)
    print('Member added')
    application()


def application():
    print('=' * 33)
    print('Movie Lover\'s club')
    print('=' * 33)
    print('1. Display all club_members.')
    print('2. Display all movie information for a member.')
    print('3. Increment the times a specific movie was watched by a member.')
    print('4. Add a movie for a member.')
    print('5. Add a new member.')
    print('6. Quit')
    print('=' * 33)

    # get name input for selection
    name_selection = (input('Please enter a selection: '))

    # if statement directing name choice to corresponding method
    if name_selection == '1':
        option_one()

    if name_selection == '2':
        option_two()

    if name_selection == '3':
        option_three()

    if name_selection == '4':
        option_four()

    if name_selection == '5':
        option_five()

    if name_selection == 'Q':
        print('=' * 33)
        print('Thank you for using Movie Lover\'s Club')
        print('=' * 33)
        sys.exit()
    else:
        input('Pick a number between 1 and 5 or Q to exit program. Press enter to continue.')


application()


上面的代码按预期工作,但不会添加电影标题,只会覆盖它们。非常感谢您的帮助。

Edit2:通过更新的代码,以下是您问题的解决方案。事实上,您非常接近,唯一的问题是您使用的是
电影
词典上的
.update
,而不是
电影[name]
词典,因此每次都会用新的电影dict替换
电影[name]
。这里的解决方案是更新
movies[name]
。此外,我还对您的
option_five
函数进行了更改,以便在添加新成员时,默认情况下,他们有一个空字典,而不是一个空字符串,因此可以对其进行更新:

def option_four():
    name = input('Please enter the member\'s name: ')

    # if the name exists in movies add the movie
    if name in movies:
        # enter information and update dictionary
        movie_title = input('Enter movie name: ')
        times_watched = int(input('Enter times watched: '))
        rating = input('Enter rating: ')

        # notice how I got rid of {name: ...} and instead only used the movie
        new_movie = {movie_title: {'Watched': times_watched, 'Rating': rating}}
        # now update the user's movie dict rather than the entire movies dict
        movies[name].update(new_movie)

    # if name not in movies print member not found call option 4 again
    else:
        print('Member not found')
        option_four()
    application()

def option_five():
    name = input('Enter new member name: ')
    nameDict = {name: {}} # notice that I replaced '' with {}

    # update the movies dictionary with name dictionary as key
    movies.update(nameDict)
    print('Member added')
    application()

现在,您可以随意为用户添加电影,而无需覆盖。

之所以会被覆盖,是因为您每次要添加新电影时都会覆盖成员列表。具体而言,当您执行以下操作时:

add_title = movies[name] = {movie_title: {'Watched': '', 'Rating': ''}}
movie[name]
现在只包含
{movie\u title:{'wasted':'','Rating':''}

以下是您的代码的新版本:

def option_four():
    name = input('Please enter the member\'s name: ')

    # enter information 
    movie_title = input('Enter movie name: ')
    times_watched = int(input('Enter times watched: '))
    rating = input('Enter rating: ')

    if name not in movies:
        movies[name] = {}

    #create the movie title value dictionary make 'movie_title input the name of dict
    movies[name][movie_title] = {'Watched': '', 'Rating': ''}

    #add watched and rating values to the movie_title dict
    movies[name][movie_title]['Watched'] = times_watched
    movies[name][movie_title]['Rating'] = rating

你能举一个电影的例子吗?为什么在你不使用它们的时候,在最后几句话的开头加上标题、手表和电影?因为你每次都看得太多了:
movies[name]={movie\u title:{'Watched':'','Rating':'}
除了“movies”命令外,整个过程都是建立在用户输入的基础上的,该命令类似于:movies={name:{movie_title:'waved',rating}}。所以基本上,名字,电影标题,观看和评级都是由用户输入定义的。我头脑中的布局是dict={nameDict:{titleDict:value,value}@MohamedMoselhy我正在尝试建立字典,以便我可以将用户输入添加到我创建的那些值中。@awarier99我在看到您的答案之前发布了它。我认为只要一个键不同,它就不会被覆盖,并且每部电影都应该是不同的标题,那么它是否应该将标题添加到电影键中?我在这里遗漏了什么吗?那也行,除非你给电影[名称]每部重新分配一本新字典time@princedakkar我更新了我的答案,这应该适合你的目的电影={}电影={名字:{movieTitle:'waved,rating}}是我试图创造的。这就是为什么要设计一本字典的正确原因吗?也许这就是我做错的地方?@princedakkar我刚刚更新了我的帖子,做了我认为你想做的事情do@princedakkar这样,如果您已经为用户准备了一本词典,则可以使用存储在
电影标题下的新词典对其进行更新。如果那里还没有字典,您可以添加一个新字典,并将
电影标题
作为键。因此,您将得到结构
movies={'Bob':{'Batman':{'wasted':5,'Rating':10},'Inception':{'wasted':3,'Rating':10}
。这对你有用吗?是的,我完全理解了这种结构,当事情变得混乱时,我尝试使用用户输入的变量。我更改了它,使添加的每个新成员在默认情况下都有一个空的dict,而不是一个空字符串,所以你可以调用dict上的
.update
,以防止出现确切的错误