Just define a main function:
import sys def main(): while True: play_songs() if __name__ == '__main__': sys.exit(main())That way you get the return code of the script.