Discuss / Python / 养成try的好习惯

养成try的好习惯

Topic source

Nil_大黄

#1 Created at ... [Delete] [Delete and Lock User]
def get_score_in(low, high):
    ' 返回指定分数区间的名字,按分数从低到高排序 '    try:
        conn = sqlite3.connect(db_file)
        cursor = conn.cursor()
        cursor.execute('select name from user where score >=? and score <=? order by score', (low, high))
        values = cursor.fetchall()
        return list(map(lambda v: v[0], values))
    except BaseException as e:
        print('Conn Error!', e)
    finally:
        cursor.close()
        conn.close()

DT_Ruan

#2 Created at ... [Delete] [Delete and Lock User]

好像有个小错误,tuple里索引数字应该是1才能引用名字

return list(map(lambda v: v[1], values))

AlaGeek

#3 Created at ... [Delete] [Delete and Lock User]

索引没错啊,二楼自己弄错了吧

随风0803

#4 Created at ... [Delete] [Delete and Lock User]

索引错乱, v[1]才是名字

恢弘腾达

#5 Created at ... [Delete] [Delete and Lock User]
如果select × 的话,索引是1,如果select name的话,索引是0。

  • 1

Reply