Try This: Use Keys to Change Values

Earlier, you used statements to add key-value pairs that were not already in the dictionary.  If a dictionary already has a given key, those statments can be used to change the value that’s paired with the key.  

  • Add the statements below to the end of the interactive_dictionary script.
  • Rename it interactive_dictionary_try_this, and save it.
  • Flash it to the micro:bit.
print("Enter game high score instead: ")
text = input("Enter high score: ")
value = int(text)

info['score'] = value

print("Updated dictionary: ")
print("info = ", info)
  • Click Open Serial and reset the micro:bit.
  • To make it match the example shown here, enter the same values that you did before, and then enter 1234 when prompted to enter high score.
  • Verify that the updated dictionary that gets displayed is info = {'name': 'Sir Lancelot', 'score' : 1234}.

Pay close attention to the difference between the first and second time the script executes this statement:

info['score'] = value

The first one in the script added the key-value pair 'score' : 1000 to the info dictionary.  The second one changed that key value pair to 'score' : 1234.

Did You Know?

This kind of dictionary isn't alphabetical! The order that the key-value pairs are in doesn’t matter.  You will always use a key to get the value paired with it.

For example, it doesn’t matter which one of these dictionaries you use, info['score'] still returns 1234.

info = {'name': 'Sir Lancelot', 'score' : 1234}
info = {'score' : 1234, 'name': 'Sir Lancelot'}