Reputation: 25
the pset i'm working on has 2 parts, both of which work fine with me. however when i run these through check50 i would get 1/6. here is a link to what i should do in the pset: pset and these are the errors i get: errors.
here is my code for import.py:
from cs50 import SQL
from sys import argv, exit
import csv
db = SQL("sqlite:///students.db")
if len(argv) != 2:
print("error")
exit(1)
if argv[1] != "characters.csv":
print("error")
exit(1)
with open(argv[1], "r") as CSVfile:
CSVfile = csv.DictReader(CSVfile, delimiter = ',')
chars = list(CSVfile)
for row in chars:
name = row["name"]
birth = row["birth"]
house = row["house"]
full = name.split( )
if len(full) == 2:
first = full[0]
last = full[1]
db.execute("INSERT INTO students(first, last, house, birth) VALUES(?, ?, ?, ?)", first, last, house, birth)
elif len(full) == 3:
first = full[0]
middle = full[1]
last = full[2]
db.execute("INSERT INTO students(first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)", first, middle, last, house, birth)
here is my code for roster:
from cs50 import SQL
from sys import argv, exit
import csv
if len(argv) != 2:
print("error")
exit(1)
db = SQL("sqlite:///students.db")
results = db.execute("SELECT * FROM students WHERE house = ? ORDER BY last ASC, first ASC", argv[1])
for row in results:
if 'middle' != None:
print(f"{row['first']} {row['middle']} {row['last']}, born {row['birth']}")
else:
print(f"{row['first']} {row['last']}, born {row['birth']}")
i've already talked to a mentor about it and i've fixed the mistakes that he has mentioned so they're irrelevant. thanks in advance.
edit:
i removed this part:
if argv[1] != "characters.csv":
print("error")
exit(1)
as it is a mistake to hardcode the name of the csv file into my code. this has improved things a lot. however, i now have faced a problem in roster. what i meant to see as output was the student's full name and birth year. some students don't have a middle name so i tried to make an if condition to see if 'middle' exists in this name or not:
if 'middle' != None:
print(f"{row['first']} {row['middle']} {row['last']}, born {row['birth']}")
else:
print(f"{row['first']} {row['last']}, born {row['birth']}")
however, what i would instead get as input for students with no middle name would be something like this: Lavender None Brown, born 1979
Upvotes: 1
Views: 73
Reputation: 6520
From the spec:
Your program should accept the name of a CSV file as a command-line argument.
If the incorrect number of command-line arguments are provided, your program should print an error and exit.
You may assume that the CSV file will exist, and will have columns name, house, and birth.
The name of the CSV file should not be hardcoded, as it is here if argv[1] != "characters.csv":
But wait there's more! (after question edit)
This if 'middle' != None:
is always true! Why? Because 'middle'
is a string literal. Maybe you meant row['middle']
?
Upvotes: 1