嗨,大家好我是新來的編碼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))