[Solved] Tuple Errors Python


It is saying that on this line:

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

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]]

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

1

solved Tuple Errors Python