簡體   English   中英

我在 python 中將項目添加到我的列表時遇到問題

[英]I am having trouble with adding items to my list in python

正如標題所說,由於某種原因,我無法將項目添加到列表中。 我相信這很簡單,但只是為了確保。

fnamesfile = open('fnames.txt','w')
global fnames
fnames = ['spanish.db', 'biology.db', 'dictionary.db']
def choose():
    global filename
    print "\nEXISTING FILES:"
    for i in fnames:
        print i
    print "\nFOR NEW FILE ENTER NEW NAME:"
    choice = raw_input('Open: ')
    choicedb = "%s.db" % choice
    if choicedb in fnames:
        filename = shelve.open(choicedb)
    else:
        choice = raw_input("ARE YOU SURE YOU WANT TO MAKE NEW FILE?[y/n] ")
        if choice == 'y':
            fnames.append(choicedb)
            filename = shelve.open(choicedb)
        elif choice == 'n':
            choose()

看起來您需要導入貨架庫並實際調用您的函數。

import shelve
fnames = ['spanish.db', 'biology.db', 'dictionary.db']
def choose():
    print fnames # for debugging
    global filename
    print "\nEXISTING FILES:"
    for i in fnames:
        print i
    print "\nFOR NEW FILE ENTER NEW NAME:"
    choice = raw_input('Open: ')
    choicedb = "%s.db" % choice
    if choicedb in fnames:
        filename = shelve.open(choicedb)
    else:
        choice = raw_input("ARE YOU SURE YOU WANT TO MAKE NEW FILE?[y/n] ")
        if choice == 'y':
            fnames.append(choicedb)
            filename = shelve.open(choicedb)
        elif choice == 'n':
            choose()
    print fnames # for debugging

choose()

我為您留下的第一個和最后一個print語句,以便您可以看到它正在工作,但是在您對它的工作感到滿意后,您應該刪除它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM