Следите за последней категорией - что-то в этом роде.
previous_category = 0
while True:
#some stuff
if 0.01 < joystick.get_axis(1) <= 0.25:
if previous_category != 1:
print ('moving backward with 25% speed')
previous_category = 1
# performing some actions
elif 0.25 < joystick.get_axis(1) <= 0.5:
if previous_category != 2:
print ('moving forward with 50% speed')
previous_category = 2
# performing some actions
elif 0.5 < joystick.get_axis(1) <= 0.75:
if previous_category != 3:
print ('moving backward with 75% speed')
previous_category = 3
# performing some actions
Если вы немного переформатировали свой код, я думаю, что это был бы лучший подход:
previous_category = 0
while True:
val = joystick.get_axis(1)
if 0.01 < val <= 0.25:
category = 1
#add 2 elif for the other categories, 2 and 3
if category == 1:
# performing some actions
elif category == 2:
# performing some actions
elif category == 3:
# performing some actions
#now that we've moved the object, we check if we need to print or not
if category != previous_category:
print_statement(category)
#and we update previous_category for the next round, which will just be the current category
previous_category = category
def print_statement(category):
#handle printing here based on type, this is more flexible