Did You Know: Many Ways to Repeat
After the AI learned what it needed to from Navigation with the cyber:bot, you might have expected it to use a for… loop to repeat the same maneuver four times. But instead, it repeated the forward-left maneuver four times! It would be better to write it as shown here:
Your Turn
Do you think maybe Bing Chat can be prompted to do a better scripting job?
- Examine the chat and test the AI generated script below.
User
That's a pretty long script. Couldn't you use a loop to get that done in fewer lines?
Bing Chat
Searching for: Python script cyber:bot travel in a square
Generating answers for you…
Yes, you can use a loop to make the script more concise. Here is a modified version of the script that uses a for loop to repeat the forward and right turn movements four times:
Python
AI-generated code. Review and use carefully. More info on FAQ.
from cyberbot import * import time # Define the duration of the forward and right turn movements forward_duration = 1.25 # seconds turn_duration = 0.85 # seconds # Repeat the forward and right turn movements four times for i in range(4): # Roll forward for 1250 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(-75) # Right wheel clockwise time.sleep(forward_duration) # Rotate right in place for 850 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(75) # Right wheel counterclockwise time.sleep(turn_duration) # Stop the servos bot(18).servo_speed(None) bot(19).servo_speed(None)
This script defines the duration of the forward and right turn movements using variables, then uses a for loop to repeat the movements four times. The range(4) function generates a sequence of numbers from 0 to 3, which are used as the loop variable i. The loop repeats the forward and right turn movements four times, then stops the servos.
I hope this helps!
Learn more:
1. learn.parallax.com
- Once again, save a copy of your current work.
- Rename your script ai-forward-right-stop-loop, and replace the text with the new chatbot script provided above.
- Connect the cyber:bot, run the script, and verify that it works.
- If you have access to Bing Chat, try your hand at making it write a script. Be as descriptive as you can in your initial request to make it write the script in one or two iterations instead of three.