元组错误 Python [英] Tuple Errors Python

查看:33
本文介绍了元组错误 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开 Python 并尝试运行以下脚本(顺便说一句,这个脚本是直接提供给我的,我没有以任何方式对其进行编辑,因为除了输入用户名和密码之外,它是作业的一部分)星号是):

I opened Python and attempted to run the following script (btw, this script is what was directly given to me, I haven't edited it in any way as it's part of an assignment aside from entering the username and the password where the astericks are):

import pymysql
myConnection  = pymysql.connect(host='localhost', user='****', passwd='****', db='accidents')
cur = myConnection.cursor()
cur.execute('SELECT vtype FROM vehicle_type WHERE  vtype LIKE "%otorcycle%";')
cycleList = cur.fetchall()
selectSQL = ('''
                SELECT  t.vtype, a.accident_severity
                FROM accidents_2016 AS a
                JOIN vehicles_2016 AS v ON  a.accident_index = v.Accident_Index
                JOIN vehicle_type AS t ON  v.Vehicle_Type = t.vcode
                WHERE t.vtype LIKE %s
                ORDER BY  a.accident_severity;''')
insertSQL = ('''INSERT INTO accident_medians  VALUES (%s, %s);''')

for cycle  in cycleList:
    cur.execute(selectSQL,cycle[0])
    accidents = cur.fetchall()
    quotient, remainder =  divmod(len(accidents),2)
    if  remainder:
        med_sev =  accidents[quotient][1]
    else:
        med_sev =  (accidents[quotient][1] + accidents[quotient+2][1])/2
    print('Finding median  for',cycle[0])
    cur.execute(insertSQL,(cycle[0],med_sev))
myConnection.commit()
myConnection.close()

我使用 pymysql 进行了导入并通过命令行安装了它.此外,由于其他错误,在阅读了一些其他回复后,我也安装了流行密码.每次运行脚本时,都会出现一个新错误.现在当我运行它时,它给了我一个不同的错误:

I did the import with the pymysql and installed it via the command line. Additionally, after reading a few other responses due to other errors, I installed the pop cryptography as well. Each time I run the script, I get a new error. Now when I run it, it gives me a different error:

Traceback (most recent call last):
  File "H:/School/ITS 410/Mod 8/Portfolio.py", line 22, in <module>
    med_sev =(accidents[quotient][1] + accidents[quotient+2][1])/2
IndexError: tuple index out of range

我只见过一次,它也是用 Python 编写的,但我不记得它是什么意思或我是如何修复它的.

I have only seen this one other time and it was also in Python but I don't remember what it means or how I fixed it.

推荐答案

在这一行上是这么说的:

It is saying that on this line:

med_sev =(accidents[quotient][1] + accidents[quotient+2][1])/2

您正在尝试索引不存在的内容.我想它是 accidents[quotient+2][1] 部分,因为这是更大的索引.这是什么意思?那么假设事故就像

you are trying to index something that doesn't exist. I imagine it is on the part accidents[quotient+2][1] because this is the greater indexed one. What does this mean? Well suppose that accidents is something like

accidents = [[thing0, thing1], [thing2, thing3]]

现在说商是 0,所以你的代码评估了事故 [2][1].这没有意义,因为事故[0] 是[thing0, thing1],而事故[1] 是[thing2, thing3],但没有事故[2].因此,当 Python 查找它并将其分配给值 med_serv 时,它不能.您可以使用以下命令验证和调试错误:

now say the quotient is 0, so you're code evaluates accidents[2][1]. This doesn't make sense because accidents[0] is [thing0, thing1], and accidents[1] is [thing2, thing3], but there is no accidents[2]. Therefore when Python goes to look find it and assign it to the value med_serv it can't. You can verify and debug the error with:

accidents = cur.fetchall()
quotient, remainder =  divmod(len(accidents),2)
if  remainder:
    print("quotient: {}".format(quotient))
    print("accidents: {}".format(accidents))
    med_sev =  accidents[quotient][1]
else:
    print("quotient: {}".format(quotient))
    print("accidents: {}".format(accidents))
    med_sev =  (accidents[quotient][1] + accidents[quotient+2][1])/2

这篇关于元组错误 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆