[Python] In this python script that checks for integer values, why is one entry not removed?...

Discussão em 'Python' iniciado por Stack, Agosto 13, 2025.

  1. Stack

    Stack Membro Participativo

    So this script takes lines from a file of lottery numbers, and is supposed to check for errors. For example, in the following:

    week 1;17,19,35,23,8,20,36
    week 2;24,28,35,8,3,22
    week x;23,29,38,1,35,18,25
    week 4;21,2,22,14,4,28,38
    week 5;5,37,20,15,3,14,9
    week 6;6,29,7,14,16,18,1
    week 7;24,31,14,23,4,3,29
    week 8;32,21,26,1,15aa,14,17
    week 9;8,13,25,12,33,34,35
    week 10;29,27,30,13,7,38,26
    week 11;34,3,7,24,16,20,38
    week 12;15,28,2,29,16,10,8
    week 13;32,22,13,14,21,28,26
    week 14;37,4,20,3,1,33,10
    week 1a5;17,8,38,18,9,32,25

    Weeks 3 and 15 should be removed for the errors in week number.

    I used an integer check to do this, and that worked with every week except week 3. I added a "print True" step, and it's not printed, but then also not removed. I asked AI, which said it was because it didn't bring up an error, but it seems to, and I get the same error result for integer checks with 'x' and '1a5'.

    def filter_incorrect():
    with open("lottery_numbers.csv") as sourcefile:
    entries = []
    for line in sourcefile:
    line = line.replace("\n","")
    line = line.replace(";",",")
    parts = line.split(",")
    entries.append(parts)
    for result in entries:

    try:
    int(result[0].lstrip("week ")) == result[0].lstrip("week ")
    print(result[0] + " True")
    pass
    except:
    entries.remove(result)
    print (len(entries))
    try:
    for i in range(1,len(result)):
    int(result) == result
    pass
    except:
    entries.remove(result)

    if len(result) != 8:
    entries.remove(result)
    try:
    for i in range(1,len(result)):
    if int(result) > 39 or int(result) < 1:
    entries.remove(result)
    except:
    pass
    for i in range(1,len(result)):
    for j in range(1,len(result)):
    if result[j] == result and i != j:
    entries.remove(result)

    print(len(entries))
    with open("correct_numbers.csv","w") as resultsfile:
    for entry in entries:
    resultsfile.write(f"{entry[0]};{entry[1]},{entry[2]},{entry[3]},{entry[4]},{entry[5]},{entry[6]},{entry[7]}\n")


    I included the whole thing but it's just lines 12 to 18 that I think I'm looking at now. I think AI gave me a different way to do it, but I'd also like to know what's wrong with this. Actually it misses some other errors, and I'm struggling to learn from my mistakes.

    Continue reading...

Compartilhe esta Página