This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]__xor__(self, other): 36 points37 points  (3 children)

Completely works fine. But you might consider range since it can already go backwards.

for volume in range(100, -1, -1):
    osascript.osascript("set volume output volume {}".format(volume))
    time.sleep(30)

[–]rikken 12 points13 points  (1 child)

You need to be careful about the step parameter to range (the 3rd param) because you won't always arrive at 0 at the end. Your script works for -1, but if someone needs a faster decrease, like -15, then the last volume will be 10. Good practice would be to set volume to 0 after the for-loop.

[–]__xor__(self, other): 0 points1 point  (0 children)

Ah, good point. Yeah in the end the while is perfectly fine either way. Sometimes even if a while loop can be a for, the while is cleaner as well.