Try This: Out of Order
Curious how a dictionary might start out as {‘start’ : 3, ‘after’ : ‘Liftoff!’} in the transmitter but end up as {‘after’ : ‘Liftoff!’, ‘start’ : 3} in the receiver? You might be able to see it by printing the dictionary immediately after the eval statement.
- Modify the countdown_receiver script by adding the statement with the # add comment shown here. Don’t change anything else in the script.
dictionary = eval(packet) print("dictionary: n", dictionary, "n") # add value = dictionary['start'] message = dictionary['after']
- Flash the receiver with the modified script.
- Enter 2 at the start prompt and Go!!! at the message prompt.
- Verity that the sender and receiver terminals resemble these:
What happened?
When you run the modified scripts and enter the start count and message after, it might look like this. The Send and Receive strings are the same {‘after’: ‘Go!!!’, ‘start’: 2}. Then, after dictionary = eval(packet), the order of the dictionary has changed to {‘start’: 2, ‘after’: ‘Go!!!’}. Each value is still paired with its key, but the order of the key-value pairs is different.
The application still worked correctly because value = dictionary[‘start’] still retrieved the 2 value that was paired with the ‘start’ key. Likewise, message = dictionary[‘after’] also retrieved the ‘Go!!!’ string value that was paired with the ‘after’ key.
When you run the modified scripts and enter the start count and message after, it might look like this. The Send and Receive strings are the same {‘after’: ‘Go!!!’, ‘start’: 2}. Then, after dictionary = eval(packet), the order of the dictionary has changed to {‘start’: 2, ‘after’: ‘Go!!!’}. Each value is still paired with its key, but the order of the key-value pairs is different.
The application still worked correctly because value = dictionary[‘start’] still retrieved the 2 value that was paired with the ‘start’ key. Likewise, message = dictionary[‘after’] also retrieved the ‘Go!!!’ string value that was paired with the ‘after’ key.