Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Make a dictionary with pairs of words in two languages (here its DK:ENG)
- dictionary = {}
- dictionary = {"Hej" : "Hello", "Hvad" : "What", "Hvem" : "Who"}
- #print a welcome message
- print("Welcome to the Danish to English translator.")
- #ask for a choice. Valid input are 1 and 2 and 0
- choice = input("Press: 1 for DK to ENG. 2 for ENG to DK. 0 to exit. ")
- while (int(choice) != 0):
- #if the user inputted 1 then translate from the original dictionary
- if choice == "1":
- #ask for a word to be translated
- word = input("Enter the word you want translated from DK to ENG: ")
- #print("The word is now: " + word)
- #newline
- print("")
- #iterate through the list and print the answer if found
- #for key, value in dictionary.items():
- for key in dictionary:
- #print("1. The word is now: " + word + " And the key is now: " + key)
- if word == key:
- print("2. The translation of your written word: " + word + " is " + value)
- #Break out of your for loop
- break
- else:
- #print("4. The word is now: " + word + " And the key is now: " + key)
- #inform the user that the word doesn't exist
- print("No such word in dicionary.")
- #ask the user to instead tell you the translation
- new_word = input("Enter the translation yourself: ")
- #newline
- print("")
- #add the new pair to the dictionary
- #dictionary2 = {word : new_word}
- #dictionary [word] = new_word
- dictionary.update({word:new_word})
- print("The word and the translation has now been added to the dicionary:")
- print(dictionary)
- print("end of if statement")
- continue
- #if the user inputted 2 then invert the dictionary and translate the word as before
- if choice == "2":
- #invert the dictionary and find the translation again the same way
- dictionary = {v: k for k, v in dictionary.items()}
- #ask for a word to be translated
- word = input("Enter the word you want translated from ENG to DK: ")
- print("")
- for key, value in dictionary.items():
- if word == key:
- print("The translation of your written word: " + word + " is " + value)
- #Break out of your for loop
- break
- else:
- #ask the user to tell you the translation
- print("No such word in dicionary.")
- new_word = input("Enter the translation yourself: ")
- print("")
- #add the new pair to the dictionary
- dictionary [word] = new_word
- print("The word and the translation has now been added.")
- print(dictionary)
- break
- print("check dictionary" + str(dictionary))
- choice = input("Press: 1 for DK to ENG. 2 for ENG to DK. 0 to exit. ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement