Home > database >  How to use modulo in if-statement to execute some functions?
How to use modulo in if-statement to execute some functions?

Time:11-06

I have a simulation time, starting from zero and counting upwards. Then I have some if-statements, in which I set the speed of a motor to a specific value. I want to execute different stuff depending on the simulation time.

E.g.,

if simulation_time % 5000 <= 0:
    motorSpeed = actionList[choice1]
elif simulation_time % 10000 <= 0:
    motorSpeed = actionList[choice2]
elif simulation_time % 15000 <= 0:
    motorSpeed = actionList[choice3]
else:
    motorSpeed = dict(speedLeft=0, speedRight=0)

As seen above, my thought was, if the time is less than 5000 do according to choice1, if the time is less than 10000 do according to choice2, etc. However when the time exceeds these values and become larger than 15000 it will get stuck in the last else-statement. It will keep doing the stuff provided there. I don't want that to happen. Instead I want to go back to the first if-statement and do the job there and then to the second if-statement and so on. I know that I should use the modulo (%) operator in somehow, but I didn't manage to.

To summerize, I want to execute the first if statement for 5000 ms and the second if statement from 5000 to 10000 and the third from 10000 to 15000 and repeat. I can not reset the time. It is the time of the simulator and it should not be reset to zero.

CodePudding user response:

I might do:

motor_speed_cycle = (simulation_time / 5000) % 3
cycle_list = [actionList[choice] for choice in (choice1, choice2, choice3)]
motorSpeed = cycle_list[motor_speed_cycle]
  • The first line sets motor_speed_cycle to a value from 0 to 2. The / 5000 breaks simulation_time into the increments of 5000 that you're interested in, and then % 3 turns that into a cycle that goes from 0 to 2 and repeats.
  • The second line creates a list with indices from 0 to 2 that correspond to the three choices in actionList.
  • The third line sets motorSpeed to the appropriate cycle_list value per motor_speed_cycle. No if/elif required.

If I didn't need any of these values for anything beyond determining motorSpeed I'd probably just do it as a single expression:

motorSpeed = [
    actionList[choice] for choice in (choice1, choice2, choice3)
][(simulation_time / 5000) % 3]

Ideally I'd have had the foresight to just make actionList a list of the three actions rather than a dict with keys choice1, choice2, choice3, and then instead of turning it into a list I could just do:

motorSpeed = actionList[(simulation_time / 5000) % 3]

CodePudding user response:

You can create a different variable to track this cycle and reset it when the timer exceeds specific threshold

motor_speed_cycle = simulation_time

if motor_speed_cycle % 5000 <= 0:
    motorSpeed = actionList[choice1]
elif motor_speed_cycle % 10000 <= 0:
    motorSpeed = actionList[choice2]
elif motor_speed_cycle % 15000 <= 0:
    motorSpeed = actionList[choice3]
else:
    motorSpeed = dict(speedLeft=0, speedRight=0)
    motor_speed_cycle = int(simulation_time / 15000)
  • Related