Bobby Bob
Bobby Bob

Reputation: 21

Python - deleteing words from a list

Hi guys I am new to coding python, and I am making a English to Spanish, and Spanish to English translator. I have managed to make it so the user is able to add in words, and if they type in that word it will also translate it. However, I want the user to be able to remove words as well. I got it working so if they type in a English word that's in the list it will remove both that English word and its Spanish translation. However, could someone help me so I can make it so if they type in a Spanish word it removes the Spanish word and its English translation(because I want it to work the other way around aswell). Right now it only works if they type in a English word thats in the list.

Any help would be much appreciated.

english_list = ["fire","apple","morning","river","wind","penguin","egg","money","pen","game","book","night","ball","laugh","boat","friend","orange","teacher","water","dog","tree","grass","chair","clock","time"]
spanish_list = ["fuego","manzana","manana","rio","viento","pinguino","huevo","dinero","pluma","juego","libro","noche","bola","risa","barco","amigo","naranja","profesor","aqua","perro","arbol","hierba","silla","relog","tiempo"]
english_to_spanish = dict(zip(english_list, spanish_list))
spanish_to_english = dict(zip(spanish_list, english_list))
#Functions
def translate(word):
    translation = english_to_spanish.get(word)
    if translation:
        print("That in Spanish is: ")
        return translation
    else:
        translation = spanish_to_english.get(word)
        if translation:
            print("That in English is:")
            return translation
        else:
            return "That wasn't a option"

def add(word):
    global english_to_spanish
    global spanish_to_english
    newword = input("Please enter the word you would like to add")
    english_list.append(newword)
    print("The word '%s' has been added to the English List, enter 'show' to see." %(newword))
    newword = input("Please enter the Spanish translation of that word")
    spanish_list.append(newword)
    english_to_spanish = dict(zip(english_list, spanish_list))
    spanish_to_english = dict(zip(spanish_list, english_list))
    print("This word '%s' has been added to the Spanish List, enter 'shows' to see." %(newword))


def remove(word):
    removeword = input("Please enter the word you would like to remove")
    index = english_list.index(removeword)
    spanishword = spanish_list[index]
    english_list.remove(removeword)
    spanish_list.remove(spanishword)
    del (english_to_spanish[removeword])
    del (spanish_to_english[spanishword])
    print("The word '%s' and '%s' has been removed from the English and Spanish list, enter 'show' to see." %(removeword, spanishword))


def showhelp():
    print("""
In this dictionary you will have few options:
You can enter:
    'add' to add a new word
    'remove' to remove a word
    'show' to view either of the word lists
    'help' to get help
    'end'/'exit' to exit
    or you can type in a word to translate it
    """)

def viewwordlist():
    if word == 'show':
        wordlist = input("""
    Type 'English' to view the English word list
    Type 'Spanish' to view the Spanish word list
    Type 'Both' to view both of the word lists
    """)
        if wordlist == 'english':
            print("Here is the current English word list:")
            print(english_list)
        elif wordlist == 'spanish':
            print("Here is the current Spanish word list:")
            print(spanish_list)
        elif wordlist == 'both':
            print("Here is both the current English and Spanish word list:")
            print("Current English list:")
            print(english_list)
            print("Current Spanish list:")
            print(spanish_list)
        else:
            print("Sorry, that wasn't a option. If you need some assistant please enter 'help'")

#Main program
name = input("What is your name?").lower()
print("Welcome %s, to the English <--> Spanish Dictionary" % name)
showhelp()

while True:
    word = input("> ")
    #Adding a word
    if word == 'add':
        add(word)
    #Remove a word
    elif word == 'remove':
        remove(word)
    #Print in help
    elif word == 'help':
        showhelp()
    #Exiting
    elif word == 'end' or word == 'exit':
        print("Thanks %s for using the English <--> Spanish dictionary" % name)
        break
    #Printing word lists
    elif word == 'show':
        viewwordlist()
    else:
        print(translate(word))

Upvotes: 0

Views: 99

Answers (3)

iuysal
iuysal

Reputation: 665

Well, there are a number of flaws in your approach but if you just need to save the day, here is what you can do. Simply, for Spanish you can repeat what you do for English. Assuming that a given word can be in the both languages with different meanings, you should check both lists and remove the word from both.

def remove(word):
    removeword = input("Please enter the word you would like to remove")
    remove_english_word(removeword)
    remove_spanish_word(removeword)

def remove_english_word(removeword):
    try:
        index = english_list.index(removeword)
        spanishword = spanish_list[index]
        english_list.remove(removeword)
        spanish_list.remove(spanishword)
        del (english_to_spanish[removeword])
        del (spanish_to_english[spanishword])
        print("The word '%s' and '%s' has been removed from the English and Spanish list, enter 'show' to see." %(removeword, spanishword))
    except ValueError, e:
        pass # In this case the word doesn't exist in english_list

def remove_spanish_word(removeword):
    try:
        index = spanish_list.index(removeword)
        englishword = english_list[index]
        spanish_list.remove(removeword)
        english_list.remove(englishword)
        del (spanish_to_english[removeword])
        del (english_to_spanish[englishword])

        print("The word '%s' and '%s' has bee removed from the Spanish and English list, enter 'show' to see." %(removeword, englishword))
    except ValueError, e:
        pass # In this case the word doesn't exist in spanish_list

Upvotes: 1

Aleksandar
Aleksandar

Reputation: 3661

try this:

def remove(word):
    removeword = input("Please enter the word you would like to remove")
    spanishword = ""
    index = english_list.index(removeword)
    if (index):
        spanishword = spanish_list[index]
    else:
        index = spanish_list.index(removeword)
        spanishword = removeword
        removeword = english_list[index]

    english_list.remove(removeword)
    spanish_list.remove(spanishword)
    del (english_to_spanish[removeword])
    del (spanish_to_english[spanishword])
    print("The word '%s' and '%s' has been removed from the English and Spanish list, enter 'show' to see." %(removeword, spanishword))

Upvotes: 0

Peeyush
Peeyush

Reputation: 726

you can modify your remove method as follows

def remove(word):
    removeword = input("Please enter the word you would like to remove")
    try:
        index = english_list.index(removeword)
        spanishword = spanish_list[index]
        english_list.remove(removeword)
        spanish_list.remove(spanishword)
        del (english_to_spanish[removeword])
        del (spanish_to_english[spanishword])
        print("The word '%s' and '%s' has been removed from the English and Spanish list, enter 'show' to see." %(removeword, spanishword))
    except ValueError:
        index = spanish_list.index(removeword)
        englishword = english_list[index]
        spanish_list.remove(removeword)
        english_list.remove(englishword)
        del (english_to_spanish[englishword])
        del (spanish_to_english[removeword])

Upvotes: 0

Related Questions