I’m trying to code the robot to randomly choose to go left or right in the Wall Maze + Playground, using this code in an if/elif/else statement.
random_direction = random.choice([LEFT,RIGHT)])
drivetrain.turn(random_direction)
I keep getting an error message though that it doesn’t like choice. I looked through the code elements and did not find a random block. I assume the error is because there is no defined choice function. Is there a way to code the random function in VexVR?
Thanks!
Michelle
1 Like
Sorry, I realized I had an extra parathesis…
1 Like
Sounds good! If you need more help please let us know!
2 Likes
Actually, I corrected that error and it runs but the code is not randomly choosing Left or Right.
I altered the Wall Maze + playground and added B to be a t-junction. In the code, if both the left and right distance sense > 300 MM, I coded random_direction to randomly choose LEFT RIGHT and then feed that into drivetrain.turn(random_direction) . After it does, I jsut have it going forward 300mm to see if it works. Each time I get a crash, which I assume is because it’s not turning before going the 300mm. I have attached both the altered playground and the python file. How can I get the robot to turn randomly left or right? Incidentally, I am starting at A, (I assumed you knew that but wanted to be 100% clear).
Thanks!
Michelle
MSherry-MazeSolver-REVISED1.vrpython (2.2 KB)
1 Like
Check that Lauren, I figured it out with the code below. I needed to use a turn_for command and input the value from random_direction.
if left_distance.get_distance(MM) and right_distance.get_distance(MM) > 300:
random_direction = random.choice([LEFT,RIGHT])
drivetrain.turn_for(random_direction,90,DEGREES)
wait(1,SEC)
drivetrain.stop()
drivetrain.drive_for(FORWARD,300,MM)
Sorry to trouble you!
Michelle
1 Like
Hi @Michelle_Sherry! No problem at all! I appreciate all of your questions I’m glad it worked out!
1 Like
Do either of yall know how to do this with blocks? Want to show my beginner students but I can’t figure it out myself.
1 Like
You could use the following block code to mimic the random turning left or right. And basically the pick random will choose between 1 and 2, if it is 1
, it will turn right
, otherwise, turn left
.
I also removed some wait commands for simplicity purpose.
1 Like