On 26/10/2013 04:09, Sven Hennig wrote:
Hey Guys,

i'm running Python 3.3.2 on Windows 7 64 Bit

I am writing a little script for practice and got a little problem.
I wrote a class which got two points in the constructor (p1 and p2).
With the function distanceOf from my class i measure the distance
between these two points. Everything works fine. But when i want to use
input to get the points i does not work...

So how can i get an int tuple with input?

Some Code:

class points:
     def __init__(self, p1, p2):
         self.p1 = p1
         self.p2 = p2
     def distanceOf(self):
         diff = (self.p2[0] - self.p1[0], self.p2[1] - self.p1[1])
         a = diff[0]
         b = diff[1]
         result = math.sqrt(a**2 + b**2)
         return "The distance between the two points:", round(result)

When i type test = points((25.0, 30.0), (40.0, 55.0)) and
test.distanceOf() everything is ok. Now i wont to get input. (In the
input prompt i write: (25.0, 30.0)
p1 = input('Please type in some coordinates')
p2 = input('Please type in some coordinates')
test = points(p1, p2)
points.distanceOf()

Traceback (most recent call last):
   File "lines.py", line 16, in <module>
     line.distanceOf()
   File "lines.py", line 6, in distanceOf
     diff = (self.p2[0] - self.p1[0], self.p2[1] - self.p1[1])
TypeError: unsupported operand type(s) for -: 'str' and 'str'

i get this error

can anyone help me out? How can i get an tuple with int values from user
input?

Greetings


Twp options from the top of my head. Either enter the coordinates separately and build your tuples (or lists) before passing them to your points class, or use the ast module literal_eval call. Note that if you choose the latter, you don't need to enter the parentheses, it's the comma that makes the tuple.

--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to