Creating a glossary without using tupler or dictionary in python

116 views Asked by At
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)

Hi, I am trying to create a glossary in python without using tuplers or a dictionary. The glossary should be made up of two lists of strings. One list for a word and the other for the description of that word. The user will get two options, either insert a new word with a description or lookup the description of a word already in the glossary. The operations insert and lookup should be in separate functions as it is in my code. The problem I run into is that when I run the program and choose 1 to insert a word everything goes smoothly and the word and its description are inserted into the lists. But then when the menu comes back and I choose 2 to look up the word I just inserted I run into problems when I try to transfer the lists into the lookup function. Because when I try to get the return values from the insert function it calls that function again. So my problem is basically to transfer the edited lists from the insert function to the lookup function without running the insert function again. As you can see the lookup function is far from finished but I got stuck on this problem.

1

There are 1 answers

1
Grape Dragon On BEST ANSWER

There are a lot of problems with this code and I apologize if I don't explain them properly, I'm still relatively new to the world of python.

There is no central hub that stores the lists "words" and "descriptions" currently in your insert function. When you call the function you're not actually assigning any variables to them. Every time you call the function you overwrite whatever you added to those lists with an empty list. Furthermore, by calling the function insert within the lookup function, you are not calling the values you want. You are running the whole function which is not what you want to happen.

I'm going to start by changing your glossary function to act as a the central hub of the program. Notice that the lists "words" and "descriptions" are outside of the while loop so they can retain information. I've also changed "if answer == "3"" to break the loop instead of returning nothing. Finally, when calling insert and lookup, I give them variables to use within the functions.

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")

The function insert looks a bit different as well, as said earlier, the lists "words" and "descriptions" are removed from the function. Also when the function is called it is given variables to work with. This function has no return statement, as far as I'm aware this is fine. If you really want one it can be changed to have one.

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)

Finally, the lookup function. It looks very different. It no longer calls another function within it, and it mostly acts as a function to print out the lists.

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

And of course we call the function glossary to run the program.

glossary()

The whole code:

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()

This is a very strange problem and I apologize if I didn't properly understand what you asked. I also changed some of the printouts for my sanity. I have no idea what you want to do with this code, but I think an interesting way of taking the value of specific portions of "words" and "descriptions" for editing or printing could be to access them by index.