データベースにクエリを実行するプログラムを作成しようとしています。データベースは golfDB であり、name (プレーヤーの名前)、totalGross (各ラウンドの総スコアの合計)、totalRounds (プレーされたラウンドの数)、pars (行われたパーの合計数) の 5 つのフィールドを持つ、players と呼ばれる 1 つのテーブルで構成されます。 、およびバーディー (作成されたバーディーの総数。私のプログラムは、最も多くのパーを持つプレーヤー、入力されたプレーヤーの平均スコア (totalGross/totalRounds) を出力し、合計グロス スコアの順にプレーヤーをリストする必要があります。 . 今のところ、ほとんどのパー数関数またはスコアを並べ替える関数を使用してプレーヤーに実際に取り組んでいません. 平均スコア関数に問題があります. このエラーが発生しており、どうすればよいか本当にわかりません修理する:
Traceback (most recent call last):
File "/Users/tinydancer9454/Documents/python/golfDBuserInterface.py", line 46, in <module>
main()
File "/Users/tinydancer9454/Documents/python/golfDBuserInterface.py", line 40, in main
queryDBavgScore(cursor)
File "/Users/tinydancer9454/Documents/python/golfDBuserInterface.py", line 29, in queryDBavgScore
answer = totalGrossScore/ totalRoundsScore
TypeError: unsupported operand type(s) for /: 'tuple' and 'tuple'
これまでの私のコードは次のとおりです。
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 from players where pars >= 0')
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/ totalRoundsScore
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()