0

I am experiencing errors on the last two check50 test cases. I checked my csv output and it seems correct to me. Anyone knows what I have done wrong in my code?

import sys
import csv

def scourgify(fileName, fileOutput):
    with open(fileName, "r") as file:
        dict, names, after = [], [], []
        reader = csv.DictReader(file, fieldnames = ["name", "house"])
        for row in reader:
            dict.append(row)
        for i in range(len(dict)-1):
            name = (dict[i+1]["name"].split(", "))
            after.append([name[1], name[0], dict[i+1]["house"]])

    with open(fileOutput, "a", newline = "") as file2:
        writer = csv.DictWriter(file2, fieldnames = ["first", "last", "house"])
        writer.writerow({"first": "first", "last": "last", "house": "house"})
        for j in range(len(after)):
            writer.writerow({"first": after[j][0], "last": after[j][1], "house": after[j][2]})

    return None

def main():
    try:
        if len(sys.argv) > 3:
            sys.exit("Too many command-line arguments")
        elif len(sys.argv) < 3:
            sys.exit("Too few command-line arguments")
        else:
            scourgify(sys.argv[1], sys.argv[2])
    except FileNotFoundError:
        sys.exit("Could not read " + sys.argv[1])

if __name__ == "__main__":
    main()

Here is what is being outputted to after.csv:

first,last,house
Hannah,Abbott,Hufflepuff
Katie,Bell,Gryffindor
Susan,Bones,Hufflepuff
Terry,Boot,Ravenclaw
Lavender,Brown,Gryffindor
Millicent,Bulstrode,Slytherin
Cho,Chang,Ravenclaw
Penelope,Clearwater,Ravenclaw
Vincent,Crabbe,Slytherin
Colin,Creevey,Gryffindor
Dennis,Creevey,Gryffindor
Cedric,Diggory,Hufflepuff
Marietta,Edgecombe,Ravenclaw
Justin,Finch-Fletchley,Hufflepuff
Seamus,Finnigan,Gryffindor
Anthony,Goldstein,Ravenclaw
Gregory,Goyle,Slytherin
Hermione,Granger,Gryffindor
Angelina,Johnson,Gryffindor
Lee,Jordan,Gryffindor
Neville,Longbottom,Gryffindor
Luna,Lovegood,Ravenclaw
Remus,Lupin,Gryffindor
Draco,Malfoy,Slytherin
Scorpius,Malfoy,Slytherin
Ernie,Macmillan,Hufflepuff
Minerva,McGonagall,Gryffindor
Eloise,Midgen,Gryffindor
Cormac,McLaggen,Gryffindor
Graham,Montague,Slytherin
Theodore,Nott,Slytherin
Pansy,Parkinson,Slytherin
Padma,Patil,Gryffindor
Parvati,Patil,Gryffindor
Harry,Potter,Gryffindor
Tom,Riddle,Slytherin
Demelza,Robins,Gryffindor
Newt,Scamander,Hufflepuff
Horace,Slughorn,Slytherin
Zacharias,Smith,Hufflepuff
Severus,Snape,Slytherin
Alicia,Spinnet,Gryffindor
Pomona,Sprout,Hufflepuff
Dean,Thomas,Gryffindor
Romilda,Vane,Gryffindor
Myrtle,Warren,Ravenclaw
Fred,Weasley,Gryffindor
George,Weasley,Gryffindor
Ginny,Weasley,Gryffindor
Percy,Weasley,Gryffindor
Ron,Weasley,Gryffindor
Oliver,Wood,Gryffindor
Blaise,Zabini,Slytherin

Here is my check50 result:https://submit.cs50.io/check50/1266cda212440a9c6e4d878b19526536872c2a8d

2
  • Does this "Did you mistakenly open your file in append mode?" from check50 refer to this with open(fileOutput, "a", newline = "") as file2:? Sep 29, 2022 at 20:34
  • @DinoCoderSaurus Yes, I supposed so - though I am not sure why exactly is this considered as a mistake.
    – dkevin77
    Sep 30, 2022 at 3:36

1 Answer 1

1

check50 tells it all "Did you mistakenly open your file in append mode?". Program does open the output file in append mode. This is a mistake when the output file already exists and contains data. One can assume this is the case on the check50 server.

1
  • Ah right! Thanks a lot for your help
    – dkevin77
    Oct 1, 2022 at 4:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .