诚然,这个文件中的代码非常难看,但它仍然可以工作,并完成了概述的任务。不过,检查器正在查找错误。我将包括链接到那个。我的代码在下面。至于他们为什么认为输出是空白的想法,那就太棒了!
来自检查者:
"Expected Output:
[{'first': 'Luna', 'middle': None, 'last': 'Lovegood', 'house': 'Ravenclaw', 'birth': 1981}]
Actual Output:
[]"
https://submit.cs50.io/check50/10dd5ef79130cbd984475a9eaac72a8c3cf8f027
import sys
from sys import argv
from cs50 import SQL
import csv
# obtain file input
if len(sys.argv) != 2:
print("Enter one CSV file")
sys.exit(1)
# read file into reader
wizards = open(argv[1])
reader = csv.reader(wizards)
# create empty lists
fullnames = []
houses = []
yob = []
namecount = 0
# save name, houses and years from each row
for row in reader:
if namecount > 0:
fullnames.append(row[0])
houses.append(row[1])
yob.append(row[2])
namecount += 1
# create empty lists
firstnames = []
middlenames = []
lastnames = []
# seperate names. couldve used .split() to do all this in hindsight
for x in range(namecount - 1):
namelen = len(fullnames[x])
tname = fullnames[x]
spaces = 0
# finds spaces in names using length
for y in range(namelen - 1):
if tname[y] == " ":
spaces += 1
nchars = 0
# loops based on spaces and puts together names before and after spaces to save in respective name lists
for s in range(spaces + 1):
tempsn = tname[nchars]
# adds chars to name until space, comma or end of full name reached
while tname[nchars] != " " and tname != "," and nchars < namelen - 1:
nchars += 1
tempsn += tname[nchars]
# saves temp names into lists
if s == 0:
firstnames.append(tempsn)
elif s == 1 and spaces == 1:
middlenames.append(None)
lastnames.append(tempsn)
elif s == 1 and spaces == 2:
middlenames.append(tempsn)
else:
lastnames.append(tempsn)
nchars += 1
# opens sql database
db = SQL("sqlite:///students.db")
# for each name, puts associated data into table using SQL query
for h in range(namecount - 1):
db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?, ?, ?, ?, ?)",
firstnames[h], middlenames[h], lastnames[h], houses[h], yob[h])
1条答案
按热度按时间oknrviil1#
让我们加入正在进行的循环。我们在“哈利”里是“y”:
它增加了空间
tempsn
.你应该听你的“分开的名字。在事后看来,我们可以使用.split()来完成这一切”