Tuple Errors Python

2024/7/5 10:55:37

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_severityFROM accidents_2016 AS aJOIN vehicles_2016 AS v ON  a.accident_index = v.Accident_IndexJOIN vehicle_type AS t ON  v.Vehicle_Type = t.vcodeWHERE t.vtype LIKE %sORDER 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])/2print('Finding median  for',cycle[0])cur.execute(insertSQL,(cycle[0],med_sev))
myConnection.commit()
myConnection.close()

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

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.

Answer

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
https://en.xdnf.cn/q/120455.html

Related Q&A

Flatten list of lists within dictionary values before processing in Pandas

Issue:If I need to flatten a list of lists I use something like this list comprehension to flatten into a single list:[item for sublist in l for item in sublist]I have a dictionary where some of the va…

how to analyse and predict(machine learning) a time series data set using scikit-learn for python

i got data-set like this i need to analyse and predict the status column. This is just 2 entrees from the training data set. In this data set there is heart rate pattern(which is collected in 1 second …

Datetime - Strftime and Strptime

Date = datetime.datetime.today().strftime("%d %B %Y") x = datetime.datetime.strptime(Date , "%d %B %Y")returns:2018-05-09 00:00:00instead of: 9 May 2018, what am I doing wrong? Ess…

Subset sum overlapping dilemma recursively

Im studying recursive logic that one of the problems is subset sum. AFAI read, there are overlaps when it is run by recursion. But, I cannot figure out how there are. Also, I read that to overcome the …

Python - download video from indirect url

I have a link like thishttps://r9---sn-4g57knle.googlevideo.com/videoplayback?id=10bc30daeba89d81&itag=22&source=picasa&begin=0&requiressl=yes&mm=30&mn=sn-4g57knle&ms=nxu&a…

Python trading logic

Heres a simple code for downloading daily stock data and computing Bollinger band indicator, but what I am not able to do is set up a logic for generating a buy and sell signal. Can someone help me wit…

Convert PDF to Excel [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Return the furthermost outlier in kmeans clustering? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Highest number of consecutively repeating values in a list

Lets say I have this list:List= [1,1,1,0,0,1,1,1,1,1]How do I display the highest number of repeating 1s in a row? I want to return 5.

Python Maze Generation

I am trying to make a python maze generator but I keep getting an IndexError: list index out of range. Any ideas? Im kinda new to this stuff so I was using the code from rosetta code on maze generatio…