[Tutor] Python Code Error

Alan Gauld alan.gauld at btinternet.com
Thu May 29 18:00:08 CEST 2014


On 29/05/14 10:48, Jude Mudannayake wrote:

> its essence is set up to take a set of data from a CSV file (containing
> x,y points) and then arrange them into coordinates points

> get an error stating the following:
>
> ‘TypeError: point1; found int, expecting tuple’ – This is referring to
> line 57 of the code which is essentially the critical command. It is a
> command that instructs to join coordinate points thus creating a line.

Don't summarize the traceback. Send the complete error text.
Summarizing usually tells us what you *think* the error is
rather than what Python is actually telling you.

> The thing is I am pretty sure I have  changed inputs into tuples from
> line 16 of the code to 19.

Maybe...

If the code is not too long (<100 lines) its usually better to embed
it in the message rather than send an attachment...

import csv
f = open ('trial.csv')
csv_f = csv.reader(f)

Ay1 = []
Az1 = []
Ay2 = []
Az2 = []


for row in csv_f:
     Ay1.append(eval(row[1]))
     Az1.append(eval(row[2]))
     Ay2.append(eval(row[3]))
     Az2.append(eval(row[4]))


As a general rule don't use eval(). It is very insecure and potentially 
masks errors because if the code is not what you expect strange things 
can silently happen. If you expect integer inputs use int(). Then if its 
not an int, python can tell you. As it stands there is a universe of 
badness that eval can interpret and not tell you a thing.

     Ay11 = tuple(Ay1)
     Az11 = tuple(Az1)
     Ay22 = tuple(Ay2)
     Az22 = tuple(Az2)

Here are the lines where you say the error occurs:


for i in range(len(xyCoordsInner)-1):
     mySketch.Line(point1=xyCoordsInner[i],
         point2=xyCoordsInner[i+1])

for i in range(len(xyCoordsOuter)-1):
     mySketch.Line(point1=xyCoordsOuter[i],
         point2=xyCoordsOuter[i+1])


Now its not obvious how we get from your lists/tuples
Ay11 etc to xyCoordsInner/Outer? Ok I found it here:

for j in range(0,len(Ay22)):
     k2 = (Ay22[j],Az22[j])
     #print k2
     myList1=k2
     xyCoordsOuter = tuple(myList1)
     #print xyCoordsOuter

This is a bizarre bit of code.
It loops over a pair of tuples.
It creates a tuple from the tuples.
It sets a second variable to point to the same tuple
It then sets a third variable to point at the same
tuple converted to a tuple(ie the same as it started)
It then goes round the loop again throwing away the
previous tuple until you finally wind up with the
last two elements as a tuple.

You could replace the entire loop with

xyXoordOuter = (Ay22[-1],Az22[-1])

Which is faster and clearer.

Of course if Ay and Az have different lengths you
get into difficulties...

I'm not sure if any of that helps but I'd definitely get
rid of the evals.

And I'd take a close look at the loop above to see
if that's really what you want to do.

And finally I'd print Point1 to see what exactly the
error is talking about.

And if you need more help, send the complete error
trace and embed the code in the message.


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list