0

Expected Output [{'first': 'Luna', 'middle': None, 'last': 'Lovegood', 'house': 'Ravenclaw', 'birth': 1981}] Actual Output: [{'first': 'Luna', 'middle': 'None', 'last': 'Lovegood', 'house': 'Ravenclaw', 'birth': 1981}]

#import.py
import sys
import csv
from cs50 import SQL

if((len(sys.argv)) != 2):
    print("Incorrect number of command line arguments")

with open(sys.argv[1],'r') as csv_file:
        csv_reader = csv.DictReader(csv_file)# read csv file into a dictionary
        db = SQL("sqlite:///students.db")
        for line in csv_reader:
            if len((line['name'].split())) == 3:
                name_list = line['name'].split()
                
                first = name_list[0]
                middle = name_list[1]
                last = name_list[2]
                house = line['house']
                birth = line['birth']
                
                db.execute("INSERT INTO students (first, middle, last, house, birth ) VALUES (?, ?, ?, ?, ?)", first, middle, last, house, birth)
            else:
                name_list = line['name'].split()
                
                first = name_list[0]
                middle = 'None'
                last = name_list[1]
                house = line['house']
                birth = line['birth']
                
                db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?, ?, ?, ?, ?)", first, middle, last, house, birth)

#roster.py
import sys
import csv
from cs50 import SQL

if((len(sys.argv)) != 2):
    print("Incorrect number of command line arguments")

house = sys.argv[1]

db = SQL("sqlite:///students.db")
rows = db.execute("SELECT first, middle, last, house, birth FROM students where house = ? order by last, first", house)
for row in rows:
    if (row['middle'] == 'None'):
        print(f"{row['first']} {row['last']}, born {row['birth']}")
    else:
        print(f"{row['first']} {row['middle']} {row['last']}, born {row['birth']}")


                
2

The None keyword is used to define a null value, or no value at all. When you put none in ' ' , it becomes a string. Remove the ' ' and it should work fine. Have a great day!.

| improve this answer | |

You must log in to answer this question.

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