[Tutor] taking a tuple with input
Sven Hennig
shennig93 at googlemail.com
Sat Oct 26 05:09:48 CEST 2013
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131026/c60985fa/attachment.html>
More information about the Tutor
mailing list