Reputation: 401
I'm trying to write a program to query a database. The database is golfDB and it consists of one table called players with 5 fields: name (player's name), totalGross (sum of the gross scores from each round), totalRounds (number of rounds played), pars (total number of pars made), and birdies (total number of birdies made. My program needs to output the player with the most pars, the average score (totalGross/ totalRounds) for an inputed player, and to list the players in order of total gross score, lowest to highest. Right now I haven't really worked on the player with the most pars function or the function to order the scores. I am having a problem with my average score function. I am getting this error, and I'm really unsure how to fix it:
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'
This is my code so far:
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()
Upvotes: 0
Views: 205
Reputation: 26362
SQLite3's fetchone returns a tuple, so you need to get the first item before you try to dived, as otherwise you would essentially be dividing two tuples.
totalGrossScore = cursor.fetchone()[0]
totalRoundsScore = cursor.fetchone()[0]
In this case your query is only getting data from one field, but keep in mind that a query may return more than just one field, and that is why fetchone returns a tuple.
Upvotes: 3