简体   繁体   中英

cs50 pset7 houses roster

My roster.py function in pset7 works as it should except for the fact that it returns 4 or 5 copies of the name and birthyear instead of just one. Through debugging you can see that the problem is with the results variable. It stores all of the copy pasted names instead of one name.

Heres my code:

import sys
from cs50 import SQL

if len(sys.argv) != 2:
    print("Needs two command-line argument")
    exit(1)

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

house = sys.argv[1]

results = db.execute("SELECT * FROM students WHERE house = ? ORDER BY last ASC, first ASC", house)


for row in results:
    if row["middle"] != None:
        print(f'{row["first"]} {row["middle"]} {row["last"]}, born {row["birth"]}')
    else:
        print(f'{row["first"]} {row["last"]}, born {row["birth"]}')

Go into your students.db, go in students table, empty your table, Then Run Following Command and make sure to run import.py once

$ python import.py characters.csv
$ python roster.py Gryffindor

if you run more than once import.py then there will be more copies and you will need to empty the table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM