Is there a better way of doing this terminal animation without having a load of if ... elif ...?import sys, timecount = 100i = 0num = 0def animation():    global num    if num == 0:        num += 1        return '[=      ]'    elif num == 1:        num += 1        return '[ =     ]'    elif num == 2:        num += 1        return '[  =    ]'    elif num == 3:        num += 1        return '[   =   ]'    elif num == 4:        num += 1        return '[    =  ]'    elif num == 5:        num += 1        return '[     = ]'    elif num == 6:        num += 1        return '[      =]'    elif num == 7:        num += 1        return '[      =]'    elif num == 8:        num += 1        return '[     = ]'    elif num == 9:        num += 1        return '[    =  ]'    elif num == 10:        num += 1        return '[   =   ]'    elif num == 11:        num += 1        return '[  =    ]'    elif num == 12:        num += 1        return '[ =     ]'    elif num == 13:        num = 0        return '[=      ]'while i < count:    sys.stdout.write('\b\b\b')    sys.stdout.write(animation())    sys.stdout.flush()    time.sleep(0.2)
Python terminal animation, better way than this?
python
You can build up the string like this:def animation(counter, length):    stage = counter % (length * 2 + 2)    if stage < length + 1:        left_spaces = stage    else:        left_spaces = length * 2 - 1 - stage    return '[' + ' ' * left_spaces + '=' + ' ' * (length - left_spaces) + ']'for i in range(100):    sys.stdout.write('\b\b\b')    sys.stdout.write(animation(i, 6))    sys.stdout.flush()    time.sleep(0.2)Alternatively store the animation strings in a tuple or list:animation_strings = ('[=      ]', '[ =     ]', '[  =    ]', '[   =   ]',                     '[    =  ]', '[     = ]', '[      =]', '[      =]',                     '[     = ]', '[    =  ]', '[   =   ]', '[  =    ]',                     '[ =     ]', '[=      ]')for i in range(100):    sys.stdout.write('\b\b\b')    sys.stdout.write(animation_strings[i % len(animation_strings)])    sys.stdout.flush()    time.sleep(0.2)You can replace the animation function with cycle from itertools:import sys, timefrom itertools import cycleanimation = cycle('[=      ]', '[ =     ]', '[  =    ]', '[   =   ]',                  '[    =  ]', '[     = ]', '[      =]', '[      =]',                  '[     = ]', '[    =  ]', '[   =   ]', '[  =    ]',                  '[ =     ]', '[=      ]')# alternatively:# animation = cycle('[' + ' ' * n + '=' + ' ' * (6 - n) + ']' #                   for n in range(7) + range(6, -1, -1))for _ in range(100):    sys.stdout.write('\b\b\b')    sys.stdout.write(animation.next())    sys.stdout.flush()    time.sleep(0.2)Finally, you could make your own generator function.def animation_generator(length):    while True:        for n in range(length + 1):            yield '[' + ' ' * n + '=' + ' ' * (length - n) + ']'        for n in range(length + 1):            yield '[' + ' ' * (length - n) + '=' + ' ' * n + ']'animation = animation_generator(6)for _ in range(100):    sys.stdout.write('\b\b\b')    sys.stdout.write(animation.next())    sys.stdout.flush()    time.sleep(0.2)EDIT: made the above suggestions less reliant on global variables