0

I have a really strange problem here: both import and rooster passes every check in check 50, but if I run Rooster the names on every house appear 4 times. I have no clue about why this is happening, here is my code:

Import:

from sys import exit
from sys import argv
from cs50 import SQL
import csv
import cs50
import sqlite3



if len(argv) != 2:

    print("missing command-line argument")
    exit(1)

 db = SQL("sqlite:///students.db")

 with open(argv[1], "r") as characters:
     reader = csv.DictReader(characters)

     for row in reader:
        name = row["name"]
        house = row["house"]
        birth = int(row["birth"])
        part = name.split()

    if len(part) == 2:
        first = part[0]
        middle = None
        last = part[1]
        db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?, ?, ?, ?, ?)",first, middle, last, house, birth)

    elif len(part) == 3:
        first = part[0]
        middle = part[1]
        last = part[2]
        db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?, ?, ?, ?, ?)",first, middle, last, house, birth)

And rooster:

 from sys import exit
 from sys import argv
 from cs50 import SQL
 import cs50
 import sys
 import sqlite3

 if len(argv) != 2:

     print("missing command-line argument")
     exit(1)

     db = SQL("sqlite:///students.db")



    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"]))

Thank you so much for your help!

1 Answer 1

1

Just check your database. I guess the data has been imported 4 times because of which each name has been printed 4 times. There is no real problem with any of your code.

Hope this helps you. If it does then please check the tickmark.

1
  • I removed stundents.db, download the first one again and then python import and rooster. It works now, so you were right, thank you so much! Commented Nov 8, 2020 at 13:45

You must log in to answer this question.

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