Condensed Code
Try This: Ask for Condensed Code
In many cases, the well documented script with each routine in a function is preferable. On the other hand, the micro:bit is a device with a small memory, so reducing the number of characters can sometimes be important.
- Test the script below against the one from Try an Existing Tilt Compass App.
- How many lines of code are in the intended solution?
- How many lines of code are in the AI’s solution.
User
Okay, that functions pretty well. Could you compress this code and make it as simple and concise as possible?
ChatGPT
Certainly! Here’s a more concise version of the code:
from microbit import * from math import atan2 while True: x, y, _ = accelerometer.get_values() angle = (atan2(y, x) * (180 / 3.14159) + 90) % 360 hour_position = round(angle / 30) % 12 display.show(Image.ALL_CLOCKS[hour_position]) sleep(100)
This version simplifies the code by removing unnecessary functions and consolidating the calculations in a more compact form. It should still provide the same functionality. Let me know if you have any further questions or if there’s anything else I can help you with!
Did You Know: How You Ask Might Matter
You might get a different code example from an AI if you phrase your explanation of how the code should work differently.
An AI might not give you the same code if you ask it a second time. Here’s what one AI has to say about that:
Anonymous
Let’s say I started a new chat and pasted the exact same first request in. Should I expect the same script, or might it be different. If it could end up being different, why would that be?
ChatGPT
If you were to start a new chat and paste the exact same first request, the generated script might be different. This is because the underlying model, while consistent in its general behavior, has some inherent randomness. The model uses probabilistic methods to generate responses, and small variations can occur.
Additionally, the model has a large number of parameters, and it might not always converge to the same solution due to the complexity of the training process. It might explore different paths to reach a similar outcome, especially for creative or open-ended tasks.
However, if the request is straightforward and the context is clear, you can generally expect similar responses. If you want a specific script or have preferences about the code structure, it’s always a good idea to provide as much detail as possible in your initial request or iterate on the instructions until you achieve the desired outcome.
Your Turn
Let’s take a closer look at how an AI might not give you the same code if you ask it a second time. That’s not necessarily a bad thing. For example, if you ask several times, it might give you a better starting point in one of the sessions. The bad side of it might be that you’ll need to evaluate each solution the AI provides and decide which one is worth working with.
Not all AIs supply a different answer each time. At the time this was written, ChatGPT 3.5 supplied four very different coding approaches with the same specification. Bing Chat appears to remember the session and answer the same way. Opening an incognito/private browser did cause it to supply a different answer with the same specification. This might also vary from one computer to the next, or maybe use a different browser on the same computer.
You can also try wording the script’s description differently, and this can also result in different coding outcomes/approaches.
- Try this yourself with ChatGPT or Bing and this description:
Please write a Python script for the micro:bit that will allow me to rotate it like a steering wheel and:
- Have a Python script use the micro:bit module’s accelerometer to measure its rotation angle.
- Always point down in its 5×5 LED display.
- Use the clock hands found in micro:bit compass examples to display the downward direction as one of twelve possible hour positions.
With ChatGPT 3.5, you will likely get different results each time. With Bing Chat, the same question might result in the same answer, but if you use a different browser or a different computer the answer might be different.
- Try rephrasing the description and asking again. Did the result change? Here is an example:
Write a Python script for the micro:bit that functions as a tilt compass. This is not the same as a compass. It should display tilt instead of north. To display tilt, it should display the direction a ball would roll off the surface of the micro:bit when titled. Use the micro:bit display API’s clock hand image display feature to point in one of twelve different directions.
- Did any of the results work on the first try?
- When you changed the wording of the description, was the script different?