在python中创建词汇表而不使用元组或字典

0mkxixxg  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(299)
def glossary():
print("Menu for glossary\n Type 1 to insert a new word\n Type 2 to lookup a word\n Type 3 to exit\n")
answer = input("Write your answer here: ")

if answer == "1":
     Insert()
elif answer == "2":
     lookup()
elif answer == "3":
     return
else:
    print("\nTry again!\n")
glossary()    

def insert():
words = []
descriptions = []
word = input("Type the word you want to add: ")
words.append(word)
description = input("Descripe the word you want to add: ")
descriptions.append(description)

return words, descriptions

def lookup():
words = insert()
descriptions = insert()

print(words,"\n", descriptions)

嗨,我正在尝试用python创建一个词汇表,而不使用元组或字典。词汇表应该由两个字符串列表组成。一个是单词列表,另一个是单词描述。用户将得到两个选项,要么插入一个带有描述的新词,要么查找词汇表中已有的词的描述。insert和lookup操作应该在单独的函数中,就像在我的代码中一样。我遇到的问题是,当我运行程序并选择1插入一个单词时,一切都很顺利,单词及其描述都被插入到列表中。但是当菜单返回,我选择2来查找我刚刚插入的单词时,当我试图将列表转换到查找函数时遇到了问题。因为当我尝试从insert函数获取返回值时,它会再次调用该函数。因此,我的问题基本上是将编辑过的列表从insert函数转移到lookup函数,而不必再次运行insert函数。正如你所看到的,查找函数远未完成,但我在这个问题上陷入了困境。

icnyk63a

icnyk63a1#

这段代码有很多问题,如果我没有正确解释,我很抱歉,我对python还是比较陌生的。
没有中央集线器可以存储插入函数中当前的“单词”和“描述”列表。当你调用这个函数时,你实际上并没有给它们分配任何变量。每次调用函数时,都会用空列表覆盖添加到这些列表中的内容。此外,通过在lookup函数中调用insert函数,您没有调用所需的值。你正在运行整个函数,这不是你想要的。
我将首先更改您的词汇表函数,使其充当程序的中心枢纽。请注意,列表“单词”和“描述”在while循环之外,因此它们可以保留信息。我还将“if answer==”3“”更改为中断循环,而不是什么都不返回。最后,在调用insert和lookup时,我为它们提供了在函数中使用的变量。

def glossary():
    words = []
    descriptions = []

    while True:
        print("\nMenu for glossary\n Type 1 to insert a new word.\n Type 2 to lookup a word.\n Type 3 to exit.\n")
        answer = input("Write your answer here: ")

        if answer == "1":
            insert(words, descriptions)
        elif answer == "2":
            lookup(words, descriptions)
        elif answer == "3":
            break
        else:
            print("\nTry again!\n")

函数insert看起来也有点不同,如前所述,列表“words”和“descriptions”从函数中删除。当函数被调用时,它也被赋予了变量。这个函数没有return语句,据我所知这很好。如果你真的想要一个,可以改成有一个。

def insert(words, descriptions):
    word = input("Type the word you want to add: ")
    words.append(word)
    description = input("Descripe the word you want to add: ")
    descriptions.append(description)

最后是查找函数。看起来很不一样。它不再调用其中的另一个函数,它主要充当打印列表的函数。

def lookup(words, descriptions):
    print('\n')
    print(*words, sep = ", ")
    print(*descriptions, sep = ", ")

当然,我们调用函数glossary来运行程序。

glossary()

整个代码:

def glossary():
    words = []
    descriptions = []

    while True:
        print("\nMenu for glossary\n Type 1 to insert a new word.\n Type 2 to lookup a word.\n Type 3 to exit.\n")
        answer = input("Write your answer here: ")

        if answer == "1":
            insert(words, descriptions)
        elif answer == "2":
            lookup(words, descriptions)
        elif answer == "3":
            break
        else:
            print("\nTry again!\n")

def insert(words, descriptions):
    word = input("Type the word you want to add: ")
    words.append(word)
    description = input("Descripe the word you want to add: ")
    descriptions.append(description)

def lookup(words, descriptions):
    print('\n')
    print(*words, sep = ", ")
    print(*descriptions, sep = ", ")

glossary()

这是一个非常奇怪的问题,如果我没有正确理解你的问题,我深表歉意。为了我的理智,我还修改了一些打印件。我不知道你想用这段代码做什么,但是我认为一个有趣的方法是通过索引访问“单词”和“描述”的特定部分的值来进行编辑或打印。

相关问题