#python 列表中嵌套字典,怎么才能打印出value值相同的字典

问题遇到的现象和发生背景

想取出两本平凡的世界,但是用for循环遍历只能取出一个,并且想要取出刘慈欣的两本书

问题相关代码,请勿粘贴截图
    print('**************图书管理系统**************')
library = [{'name':'三体','author':'刘慈欣','price':24,'number':5},
           {'name':'流浪地球','author':'刘慈欣','price':26,'number':5}
    ,{'name':'平凡的世界','author':'路遥','price':108,'number':5},
           {'name':'平凡的世界','author':'鲁瑶','price':108,'number':5}
    ,{'name':'活着','author':'余华','price':20,'number':5}]
name_list = []
author_list = []
for i in library:
    name_list.append(i.get('name'))
    author_list.append(i.get('author'))


print(name_list)
print(author_list)
while 1:
    print('1.借书\n2.还书\n3.查询\n4.书库\n5.退出')
    choice = int(input('请选择功能:'))

    if choice == 1:
        i_name = input('请输入所要借阅的书:')
        n_number = name_list.count(i_name)
        for i in range(0,n_number):
            for book in library:
                if book.get('name') == i_name:
                    print('该书的相关信息为:\n书名:{}\t作者:{}\t价格:{}元\t库存量:{}本'.format(book.get('name'), book.get('author'), book.get('price'),book.get('number')))
                    break
            else:
                print('书库中暂未收录该书')


运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

想要实现按照书名取书和按照作者名取书,并且全部取出

那就把你的代码中if分支的break去掉呀

你把for里面的break去掉就行了。
执行break会中断循环。所以搜索到一个同名打印就结束了。

为什么你的标题和你的描述是两个问题,我回答标题

a=[]
for i in range(len(library)):
    if library[i]['author']=='刘慈欣':
        a.append(library[i]['name'])
print(a)

这个改成按书名或者作者查询书籍

print('**************图书管理系统**************')


library = [{'name': '三体', 'author': '刘慈欣', 'price': 24, 'number': 5},
           {'name': '流浪地球', 'author': '刘慈欣', 'price': 26, 'number': 5}
    , {'name': '平凡的世界', 'author': '路遥', 'price': 108, 'number': 5},
           {'name': '平凡的世界', 'author': '鲁瑶', 'price': 108, 'number': 5}
    , {'name': '活着', 'author': '余华', 'price': 20, 'number': 5}]
name_list = []
author_list = []
for i in library:
    name_list.append(i.get('name'))
    author_list.append(i.get('author'))

print(name_list)
print(author_list)
while 1:
    print('1.借书\n2.还书\n3.查询\n4.书库\n5.退出')
    choice = int(input('请选择功能:'))

    if choice == 1:
        i_name = input('请输入所要借阅的书或作者:')
        n_number = name_list.count(i_name)
        flag = ''
        for book in library:
            if book.get('name') == i_name or book.get('author') == i_name :
                print('该书的相关信息为:\n书名:{}\t作者:{}\t价格:{}元\t库存量:{}本'.format(book.get('name'), book.get('author'),
                                                                        book.get('price'), book.get('number')))
                flag = 'ok'

        if flag == '':
            print('书库中暂未收录该书')

建议先确认思路,再写代码, 不然容易进入误区。
这样改一下:

print('**************图书管理系统**************')


library = [{'name': '三体', 'author': '刘慈欣', 'price': 24, 'number': 5},
           {'name': '流浪地球', 'author': '刘慈欣', 'price': 26, 'number': 5}
    , {'name': '平凡的世界', 'author': '路遥', 'price': 108, 'number': 5},
           {'name': '平凡的世界', 'author': '鲁瑶', 'price': 108, 'number': 5}
    , {'name': '活着', 'author': '余华', 'price': 20, 'number': 5}]
name_list = []
author_list = []
for i in library:
    name_list.append(i.get('name'))
    author_list.append(i.get('author'))

print(name_list)
print(author_list)
while 1:
    print('1.借书\n2.还书\n3.查询\n4.书库\n5.退出')
    choice = int(input('请选择功能:'))

    if choice == 1:
        i_name = input('请输入所要借阅的书:')
        n_number = name_list.count(i_name)
        flag = ''
        for book in library:
            if book.get('name') == i_name:
                print('该书的相关信息为:\n书名:{}\t作者:{}\t价格:{}元\t库存量:{}本'.format(book.get('name'), book.get('author'),
                                                                        book.get('price'), book.get('number')))
                flag = 'ok'

        if flag == '':
            print('书库中暂未收录该书')


你的问题不是出在if,else那里,是出在第一个循环,for i in range(0,n_number):,n_number是用户输入名字在图书馆中该名字的数量,当用户输入没有的书,n_number=0,那就是range(0,0),循环都没有进入,怎末打印没有这本书呢

 if choice == 1:
        i_name = input('请输入所要借阅的书:')
        for book in library:
            if book.get('name') == i_name:
                print('该书的相关信息为:\n书名:{}\t作者:{}\t价格:{}元\t库存量:{}本'.format(book.get('name'), book.get('author'),
                                                                        book.get('price'), book.get('number')))
                break
            else:
                print('书库中暂未收录该书')
                break#添加一个break,要不然会打印5次,一次就够了