我已经为房屋import.py和roster.py编写了代码,当我自己对其进行测试时,它们都可以正常工作并输出规范期望的输出。我遇到麻烦的地方是我提交代码的时间,即使我的代码在我的终端上正常工作,它仍然在check50(当我提交代码)上告诉我import.py根本不产生任何输出。我已附上支票50的结果以显示其内容。
谢谢
IMPORT.PY
import csv
import sys
from cs50 import SQL
if len(sys.argv) != 2:
print("Usage: python import.py characters.csv")
sys.exit(1)
db = SQL("sqlite:///students.db")
with open(sys.argv[1], "r") as characters:
reader = csv.DictReader(characters, delimiter=",")
for row in reader:
name = row["name"]
name_list = name.split()
if len(name_list) == 2:
first_name = name_list[0]
last_name = name_list[1]
db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)",
first_name, None, last_name, row["house"], row["birth"])
elif len(name_list) == 3:
first_name = name_list[0]
middle_name = name_list[1]
last_name = name_list[2]
db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)",
first_name, middle_name, last_name, row["house"], row["birth"])
ROSTER.PY
import csv
import sys
from cs50 import SQL
if len(sys.argv) != 2:
if sys.argv[1] != "Gryffindor" or sys.argv[1] != "Slytherin" or sys.argv[1] != "Hufflepuff" or sys.argv[1] != "Ravenclaw":
print("Usage: python roster.py house")
db = SQL("sqlite:///students.db")
house_list = db.execute("SELECT first, middle, last, birth FROM students WHERE house = (?) ORDER BY last, first", sys.argv[1])
for row in house_list:
if row["middle"] == None:
print(row["first"] + " " + row["last"] + ", born " + str(row["birth"]))
else:
print(row["first"] + " " + row["middle"] + " " + row["last"] + ", born " + str(row["birth"]))
答案 0 :(得分:1)
第一次错过该问题的歉意。运行check50
时,students.db中已经有一个students
表,因此CREATE语句失败。也许这太过分了,但是规范没有创建表的说明。与本课程中的所有pset一样,一个人应该做并且只能按照规范中的说明进行操作。