我正在尝试编写一个程序来查询数据库。数据库是 golfDB,它由一个名为players
5 个字段的表组成:
name
(球员姓名)totalGross
(每轮总分的总和)totalRounds
(比赛轮数)pars
(制作的总杆数)birdies
(小鸟总数)
我的程序需要输出得分最多的球员,输入球员的平均得分(totalGross/totalRounds),并按照总得分从低到高的顺序列出球员。
我真的不知道如何输出具有最多标准杆的球员。现在我的代码正在输出一个列表列表,每个列表都有一个玩家和他们的标准杆。我不知道如何按照他们的标准来排序,然后选择最高的,因为我所做的一切都只是选择数字,然后我无法回到他们属于哪个球员。
有谁知道如何仅按标准杆数对列表列表进行排序,然后它就可以打印播放器?
import sqlite3
def getDBCursor(DB):
"""obtain and return a cursor for the database DB"""
conn= sqlite3.connect('/Users/tinydancer9454/Documents/python/golfDB')
cursor= conn.cursor()
return cursor
def queryDBpars(cursor):
"""find out which player had the most pars"""
cursor.execute('select name, pars from players where pars >= 0')
playerPars= cursor.fetchall()
def queryDBavgScore(cursor):
"""find the average score of inputed player"""
player= input("Please enter the player's name: ")
cursor.execute('select totalGross from players where name = ?', (player,))
totalGrossScore = cursor.fetchone()
cursor.execute('select totalRounds from players where name = ?', (player,))
totalRoundsScore = cursor.fetchone()
answer = totalGrossScore[0]/ totalRoundsScore[0]
print('The average score for', player, 'is', answer,)
def queryDBplayers(cursor):
"""lists the players in order of their total gross score"""
def main():
"""obtain cursor and query the database: golfDB"""
cursor= getDBCursor('golfDB')
queryDBpars(cursor)
queryDBavgScore(cursor)
queryDBplayers(cursor)
cursor.close()
这是输出:
[('Ruth', 16), ('Elena', 12), ('Jane', 12), ('Ezgi', 13), ('Ricki', 9), ('Margaret', 10), ('Rosalia', 16), ('Betty', 14)]