Advertisement
Guest User

dictionary

a guest
Oct 4th, 2018
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. #Make a dictionary with pairs of words in two languages (here its DK:ENG)
  2. dictionary = {}
  3. dictionary = {"Hej" : "Hello", "Hvad" : "What", "Hvem" : "Who"}
  4.  
  5. #print a welcome message
  6. print("Welcome to the Danish to English translator.")
  7.  
  8.  
  9. #ask for a choice. Valid input are 1 and 2 and 0
  10. choice = input("Press: 1 for DK to ENG. 2 for ENG to DK. 0 to exit. ")
  11.  
  12.  
  13. while (int(choice) != 0):
  14.  
  15.     #if the user inputted 1 then translate from the original dictionary
  16.     if choice == "1":
  17.         #ask for a word to be translated
  18.         word = input("Enter the word you want translated from DK to ENG: ")
  19.  
  20.         #print("The word is now: " + word)
  21.  
  22.         #newline
  23.         print("")
  24.  
  25.         #iterate through the list and print the answer if found
  26.         #for key, value in dictionary.items():
  27.         for key in dictionary:
  28.             #print("1. The word is now: " + word + " And the key is now: " + key)
  29.  
  30.             if word == key:
  31.                 print("2. The translation of your written word: " + word + " is " + value)
  32.  
  33.                 #Break out of your for loop
  34.                 break
  35.             else:
  36.                 #print("4. The word is now: " + word + " And the key is now: " + key)
  37.  
  38.                 #inform the user that the word doesn't exist
  39.                 print("No such word in dicionary.")
  40.  
  41.                 #ask the user to instead tell you the translation
  42.                 new_word = input("Enter the translation yourself: ")
  43.  
  44.                 #newline
  45.                 print("")
  46.  
  47.                 #add the new pair to the dictionary
  48.                 #dictionary2 = {word : new_word}
  49.                 #dictionary [word] = new_word
  50.                 dictionary.update({word:new_word})
  51.  
  52.                 print("The word and the translation has now been added to the dicionary:")
  53.                 print(dictionary)
  54.  
  55.                 print("end of if statement")
  56.                 continue
  57.     #if the user inputted 2 then invert the dictionary and translate the word as before
  58.     if choice == "2":
  59.  
  60.         #invert the dictionary and find the translation again the same way
  61.         dictionary = {v: k for k, v in dictionary.items()}
  62.  
  63.         #ask for a word to be translated
  64.         word = input("Enter the word you want translated from ENG to DK: ")
  65.  
  66.         print("")
  67.  
  68.         for key, value in dictionary.items():
  69.             if word == key:
  70.                 print("The translation of your written word: " + word + " is " + value)
  71.                 #Break out of your for loop
  72.                 break
  73.             else:
  74.                 #ask the user to tell you the translation
  75.                 print("No such word in dicionary.")
  76.                 new_word = input("Enter the translation yourself: ")
  77.                 print("")
  78.  
  79.                 #add the new pair to the dictionary
  80.                 dictionary [word] = new_word
  81.  
  82.                 print("The word and the translation has now been added.")
  83.                 print(dictionary)
  84.                 break
  85.  
  86.     print("check dictionary" + str(dictionary))
  87.  
  88.     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