Я пытаюсь ответить только на вопрос 9. До этого момента код Python работал правильно.Я не могу получить какой-либо ответ с этого момента вперед.Любая помощь или помощь будет принята с благодарностью.Это большой ум, вы знаете, что для меня на данный момент.Поток стекирования специально запрашивает больше контекста, но у меня нет ничего, чтобы предоставить.Просто снова обращаюсь за помощью с вопросом 9.
import sqlite3
from contextlib import closing
# creating a connection
conn = sqlite3.connect("babynames.db")
# creating table for boys
with closing(conn.cursor()) as c :
query = """CREATE TABLE BOYS(
Rank INTEGER PRIMARY KEY,
Name TEXT NOT NULL,
[Total Babies] INTEGER NOT NULL,
[Total Percentage] REAL NOT NULL)
"""
c.execute(query)
# # creating table for girls
with closing(conn.cursor()) as c :
query = """CREATE TABLE GIRLS(
Rank INTEGER PRIMARY KEY,
Name TEXT NOT NULL,
[Total Babies] INTEGER NOT NULL,
[Total Percentage] REAL NOT NULL)
"""
c.execute(query)
# reading file contents into two separate tables
print("Adding data to database\n")
f = open("babynames.txt", "r")
for x in f:
x = x.split(" ") # Splitting each attribute
# adding new "boy name" to boys table
with closing(conn.cursor()) as c:
query = """INSERT INTO BOYS
(Rank, Name, [Total Babies], [Total Percentage])
Values(?, ?, ?, ?)
"""
c.execute(query, (int(x[0]), x[1], int(x[2]), float(x[3])))
conn.commit()
# adding new "girl name" to girls table
with closing(conn.cursor()) as c:
query = """INSERT INTO GIRLS
(Rank, Name, [Total Babies], [Total Percentage])
Values(?, ?, ?, ?)
"""
c.execute(query, (int(x[0]), x[4], int(x[5]), float(x[6])))
conn.commit()
f.close()
print("All entries have been added to the database\n")
# question 7: How many babies were born that year?
num_babies = 0
num_boys = 0
num_girls = 0
with closing(conn.cursor()) as c:
query = """
SELECT sum(b.[Total Babies]) AS TotalBoys, sum(g.[Total Babies]) AS TotalGirls
FROM BOYS b JOIN GIRLS g ON b.RANK=g.RANK
"""
c.execute(query)
res = c.fetchall()
tup = res[0]
num_boys = tup[0]
num_girls = tup[1]
num_babies = num_boys + num_girls
print("The number of babies born that year is {}\n".format(num_babies))
# question 8: Are there more boys or girls in that year?
# Doing directly using results of previous query
if(num_boys > num_girls):
print("More boys were born that year\n")
elif(num_boys < num_girls):
print("More girls were born that year\n")
else:
print("Same no. of boys and girls were born that year\n")
# question 9
# Out of the total number of babies born in that year, cumulatively speaking and starting from the
# first one, what is the name of the boy and girl that crossed 50% of the total baby born in that
# year?
with closing(conn.cursor()) as c:
query = """
SELECT b1.Rank, b1.Name, sum(b1.[Total Percentage]) AS CPercent
FROM BOYS b1, BOYS b2
WHERE b1.[Total Percentage] <= b2.[Total Percentage] OR (b1.[Total Percentage]=b2.[Total Percentage] AND b1.Rank = b2.Rank AND b1.Name = b2.Name)
GROUP BY b1.Rank, B1.Name, b1.[Total Percentage]
ORDER BY b1.[Total Percentage] DESC, b1.Rank ASC
"""
c.execute(query)
cumulative_boys = c.fetchall()
for boy in cumulative_boys:
if(boy[2] >= 50):
print("The boy who crosed 50%% of the total babies born that year is ",boy[1])
break
# Repeat above for girls, with table name altered
with closing(conn.cursor()) as c:
query = """
SELECT g1.Rank, g1.Name, sum(g1.[Total Percentage]) AS CPercent
FROM GIRLS g1, GIRLS g2
WHERE g1.[Total Percentage] <= g2.[Total Percentage] OR (g1.[Total Percentage]=g2.[Total Percentage] AND g1.Rank = g2.Rank AND g1.Name=g2.Name)
GROUP BY g1.Rank, g1.Name, g1.[Total Percentage]
ORDER BY g1.[Total Percentage] DESC, g1.Rank ASC
"""
c.execute(query)
cumulative_girls = c.fetchall()
for girl in cumulative_girls:
print(girl)
if(girl[2] >= 50):
print("The boy who crosed 50%% of the total babies born that year is ",girl[1])
break
# closing connection
conn.close()