在做作业问题并按照他们的指示键入代码后,我不得不猜测打印功能。当我运行代码时,出现元组错误,但是我已经按照他们的意愿键入了它。我可能会错了,但我无法结束
import sqlite3
def find_details(id2find):
db = sqlite3.connect("surfersDB.sdb")
dbrow_factory = sqlite3.Row
cursor = db.cursor()
cursor.execute("select * from surfers")
rows = cursor.fetchall()
for row in rows:
if row['id'] == id2find:
s = {}
s['id'] = int(row['id'])
s['name'] = row['name']
s['country'] = row['country']
s['average'] = str(row['average'])
s['board'] = row['board']
s['age'] = int(row['age'])
cursor.close()
return(s)
cursor.close()
return({})
lookup_id = int(input("Enter the id of the surfer: "))
surfer = find_details(lookup_id)
if surfer:
print("ID: " + surfer['id'])
print("Name: " + surfer['name'])
print("Country: " + surfer['country'])
print("Average: " + surfer['average'])
print("Board type: " + surfer['board'])
print("Age: " + surfer['age'])
======重新启动:C:/ Users / new / Documents / HeadFirst Python / tvn_database.py ====== 1 输入冲浪者的ID:101 追溯(最近一次通话): 文件“ C:/ Users / new / Documents / HeadFirst Python / tvn_database.py”,第23行,在 冲浪者= find_details(lookup_id) 文件“ C:/ Users / new / Documents / HeadFirst Python / tvn_database.py”,第10行,在find_details中 如果row ['id'] == id2find: TypeError:元组索引必须是整数或切片,而不是str