Did You Know: Good vs Better
ChatGPT wrote a functioning script that mostly “does the job.” However it is not as easy to understand as it could be, and it takes more memory than examples from other micro:bit tutorials. For example, it could have used Image.HEART and Image.HEART_Small instead of creating the heart_full and heart_empty custom images. Also, instead of importing the time module, it could just use sleep(500). All of these changes would make the code easier to read and understand, while using less memory. A better script.
Your Turn: Asking for Easy to Read Code
User
Why not use Image.HEART and Image.HEART_SMALL instead? Also, instead of importing time, just use sleep in milliseconds.
ChatGPT
Certainly! Here's an updated version of the program using Image.HEART and Image.HEART_SMALL, and using the sleep function in milliseconds:
from microbit import * while True: display.show(Image.HEART) sleep(500) # Display the full heart for 500 milliseconds (0.5 seconds) display.show(Image.HEART_SMALL) sleep(500) # Display the small heart for 500 milliseconds (0.5 seconds)
This program achieves the same beating heart effect using the built-in heart images and the sleep function with a delay of 500 milliseconds. Feel free to customize the timings or make any other adjustments as needed!
User
That looks better.
ChatGPT
I'm glad you found it better! If you have any more questions or if there's anything else I can help you with, feel free to ask. Happy coding with your micro:bit!
- Enter the script into the micro:bit Python Editor.
- Run the script by clicking the Send to micro:bit button.
- Did it work as ChatGPT described, by displaying beating heart?
- Was the result identical to the previous AI-generated heartbeat script from the last page?