In [4]:
import os
import random

used = "used.txt"
if not os.path.exists(used):
    open(used, "w+").close()

with open(used, "r+", encoding="utf8") as f:
    used_problems = f.readlines()
    used_problems = [u.strip("\n") for u in used_problems]
    print("Used:", used_problems)

    r = [i for i in range(1, 1040)]
    r = [i for i in r if str(i) not in used_problems]
    random.shuffle(r)
    now = None
    if len(r) == 0:
        print("All done!")
        exit(0)
    elif len(r) == 1:
        now = r[0]
    else:
        now = random.choice(r)
    print("Now:", now)
    f.write(str(now) + "\n")
Used: ['651', '774', '607']
Now: 691