大家好我是编码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))
答案 0 :(得分: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
答案 1 :(得分:0)
您可以按照以下方式修改remove
方法
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])
答案 2 :(得分:0)
试试这个:
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))