1

I am working on the CS50 course by Harvard EDx. The output for CS50 - Houses is correct in my console. However, check50 which is validates and evaluates the response gives an error. I have checked the other similar questions in stackoverlow, but I have neither CREATED TABLE nor passed "character.csv" as the argument in import.py. @DinoCodeSauraus has mentioned in a similar query that the schema is different as compared to the one in the distro. Can you please clarify this further.

I note that if I change to if sys.argv[1]!="students.csv": in import.py, the output in check50 is correct. However, it still points out that roster.py is incorrect though the output seems identical to the desired output.

Output in check50:

output in check50

Thanks for your help

import sys
from cs50 import SQL
import csv
db = SQL("sqlite:///students.db")

#   give error message if incorrect number of arguments
if sys.argv[1]!="characters.csv":
    print("wrong input")
    sys.exit(1)

with open(sys.argv[1], 'r') as csvfile:
    db.execute("DELETE FROM students")
    csvreader = csv.reader(csvfile)
    header = next(csvreader)
    index = 0
    for row in csvreader:
        index +=1
        name = row[0]
        house=row[1]
        DOB=row[2]
        names = [] *3
        names = name.split(" ")
        first_name=names[0]
        if len(names) ==2:
            last_name = names[1]
            middle_name = None
            db.execute("INSERT INTO students (id, first, middle, last, house, birth) VALUES(:index, :first, :middle, :last, :house, :birth)", index = index, first = first_name, middle = middle_name, last = last_name, house = house, birth = DOB)
        else:
            last_name = names[2]
            middle_name = names[1]
            db.execute("INSERT INTO students (id, first, middle, last, house, birth) VALUES(:index, :first, :middle, :last, :house, :birth)", index = index, first = first_name, middle = middle_name, last = last_name, house = house, birth = DOB)

This is roster.py code

import sys
from cs50 import SQL
import csv
db = SQL("sqlite:///students.db")

#   give error message if incorrect number of arguments
if len(sys.argv)!= 2 :
    print("wrong input")
    sys.exit(1)

rows = db.execute("SELECT * FROM students WHERE house = :house ORDER BY last, first", house=sys.argv[1])
for row in rows :
    if row["middle"] is None:
       print("{} {}, born {} ".format(row["first"], row["last"], row["birth"]))
    else :
       print("{} {} {}, born {} ".format(row["first"], row["middle"], row["last"], row["birth"]))

1

Fixed the errors

  1. import.py : The name check for characters.csv was too specific. Needed to check only for file format of .csv
  2. roster.py : Extra space after each line

Thanks for your help :D

| improve this answer | |

You must log in to answer this question.

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