Python全局变量未显示字典值

时间:2016-11-26 06:26:35

标签: python python-2.7 global-variables

这是我的代码(抱歉的缩进搞砸了)。

    #!/usr/bin/python

inventory = []
book = {}

# find index
def find_index(inventory, isbn, title):
    if isbn in inventory.keys():
        quantity = int(raw_input("""Add quantity: """))

def add_book(inventory):
    print("""New Book successfully added""")
    book.clear()
    return inventory

def remove_book(inventory):
    isbn = raw_input("""Enter the ISBN:""")

def find_book(inventory):
    print("""
    Search Menu
    ===================

    1. Search by Title
    2. Search by ISBN""")
    answer = input("""Enter your selection: """)

    if answer == 1:
        title = raw_input(""" Enter the title: """)
    elif answer == 2:
        isbn = raw_input(""" Enter the ISBN: """)


def show_inventory(inventory):
    print("""
    Complete Inventory:
    ===================
    """)

# menu method
def menu():
    global inventory
    selection = True
    while selection:
        print("""
        Book Inventory Menu
        ===================

        1. Add a book
        2. Remove a book
        3. Find a book
        4. Show Complete Inventory
        5. Quit""")
        answer = int(input("""
        Enter your selection: """))

        if answer == 1:
            book['isbn'] = raw_input("""Enter the ISBN: """)
            book['title'] = raw_input("""Enter the title: """)
            book['authors'] = raw_input("""Enter the author(s): """)
            book['pubyear'] = raw_input("""Enter publication year: """)
            book['edition'] = raw_input("""Enter the edition: """)
            book['publisher'] = raw_input("""Enter the publisher: """)
            book['quantity'] = raw_input("""Enter quantity: """)
            inventory.append(book)
            print(inventory)
            add_book(inventory)
        elif answer == 2:
            remove_book(inventory)
        elif answer == 3:
            find_book(inventory)
        elif answer == 4:
            print(inventory)
        else:
            print("""Goodbye!""")
            exit()

menu()

这是我的输出

    Book Inventory Menu
        ===================

        1. Add a book
        2. Remove a book
        3. Find a book
        4. Show Complete Inventory
        5. Quit

        Enter your selection: 1
Enter the ISBN: a
Enter the title: s
Enter the author(s): d
Enter publication year: f
Enter the edition: g
Enter the publisher: h
Enter quantity: j
[{'pubyear': 'f', 'isbn': 'a', 'publisher': 'h', 'title': 's', 'edition': 'g', 'authors': 'd', 'quantity': 'j'}]
New Book successfully added

        Book Inventory Menu
        ===================

        1. Add a book
        2. Remove a book
        3. Find a book
        4. Show Complete Inventory
        5. Quit

        Enter your selection: 1
Enter the ISBN: q
Enter the title: w
Enter the author(s): e
Enter publication year: r
Enter the edition: t
Enter the publisher: y
Enter quantity: u
[{'pubyear': 'r', 'isbn': 'q', 'publisher': 'y', 'title': 'w', 'edition': 't', 'authors': 'e', 'quantity': 'u'}, {'pubyear': 'r', 'isbn': 'q', 'publisher': 'y', 'title': 'w', 'edition': 't', 'authors': 'e', 'quantity': 'u'}]
New Book successfully added

        Book Inventory Menu
        ===================

        1. Add a book
        2. Remove a book
        3. Find a book
        4. Show Complete Inventory
        5. Quit

        Enter your selection: 4
[{}, {}]

当我试图显示完整的库存时,我只是在列表/数组中得到两个空白字典。我尝试循环,我仍然得到空白的说法。我是python的新手,感谢帮助。

2 个答案:

答案 0 :(得分:1)

您遇到的行为的罪魁祸首是book.clear()函数中的add_book行;然而,问题比这更深刻。 由于book是一个全局变量,因此每次将其附加到inventory数组时,实际上都会附加对同一个全局book变量的引用。由于每次添加新书时都会修改全局变量,因此库存数组最终将成为所有具有最近添加的book内容的词典。这就是为什么在第二种情况下两个条目都相同的原因。当您致电book.clear()时,您正在清除book中的条目,这些条目构成了inventory数组中的所有条目。

要解决此问题,请不要将book设为全局变量。相反,在book = {}行之后定义if answer == 1局部变量,并将其附加到全局数组,同时还删除book的全局声明。看起来应该是这样的:

if answer == 1:
        book = {}
        book['isbn'] = raw_input("""Enter the ISBN: """)
        #fill in remaining fields...

答案 1 :(得分:0)

这是因为每次添加图书并调用此函数时,您的变量book都会被清除:add_book(inventory)。只需评论该行:

book.clear()

add_book函数中。