How to change a numeric variable from positive to negative and vice versa Pro Preview

Sometimes we have a numeric variable that is an input parameter for many commands but must sometimes be a negative value. There are several ways to use this variable without changing it.

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #1552
  • 27 Feb 2020

Multiplying by negative one

One of these ways is to multiply the variable by negative one (-1) inside the input parameter. Here's an example where two run() commands use the same "motor_speed" variable with different signs:

# Create your objects here. 
ev3 = EV3Brick()

motor_speed = 500

# Write your program here. 
Motor(Port.B).run(motor_speed)
Motor(Port.C).run(motor_speed * (-1))

By subtracting from zero

Another method of doing this is to subtract the variable from 0. Here's how the program would look using this method:

# Create your objects here. 
ev3 = EV3Brick()

motor_speed = 500

# Write your program here. 
Motor(Port.B).run(motor_speed)
Motor(Port.C).run(0 - motor_speed)

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for Python with LEGO Mindstorms EV3 - Level 1
  • 74
  • 28:18
  • 114
Image for Lesson 4 - Strange Bot
  • 7
  • 5
  • 11
  • 3d_rotation 1