2014-09-04 186 views
0

嗨,大家好我是新來的編碼python,並且我正在將英語翻譯成西班牙語,並將西班牙語翻譯成英語翻譯。我設法讓用戶能夠添加單詞,如果他們輸入該單詞,它也會翻譯它。但是,我希望用戶能夠刪除單詞。我得到它的工作,所以如果他們鍵入列表中的英文單詞,它將刪除該英文單詞及其西班牙語翻譯。然而,有人可以幫助我,所以我可以做到這一點,如果他們鍵入一個西班牙語單詞,它會刪除西班牙單詞和它的英文翻譯(因爲我希望它以另一種方式工作)。現在只有在列表中輸入英文單詞時纔有效。Python - 從列表中刪除單詞

任何幫助將不勝感激。

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

回答

1

好,也有一些在你的方法的缺陷,但如果你只需要保存那一天,這是你能做的。簡單地說,對於西班牙語,你可以重複你爲英語做的事。假設一個給定的單詞可以用不同的含義在兩種語言中,你應該檢查兩個單詞並從兩個單詞中刪除單詞。

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 
0

您可以按如下

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]) 
0

試試這個修改remove方法:

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