Der er en del fejl. For det første checker du efter "value" som er udefineret, derudover er der ikke styr på loops og hvordan break og continue bliver brugt. Ved ikke helt hvorfor fejlen "Dictionary changed size" opstår, men det er unødvendigt at bruge dict.update() til bare at tilføje en ny oversættelse. Det er også nemmere at bruge "If x in dictionary" i stedet for at iterere igennem.
Med lidt modifikationer virker følgende for mig (har fjernet sidste del):
# Make a dictionary with pairs of words in two languages (here its DK:ENG)
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: ")
if word in dictionary:
print("2. The translation of your written word: %s" % dictionary[word])
else:
# 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
dictionary[word] = new_word
print("The word and the translation has now been added to the dicionary:")
print("check dictionary" + str(dictionary))
choice = input("Press: 1 for DK to ENG. 2 for ENG to DK. 0 to exit. ")
--
Sidst redigeret 04-10-2018 16:40