Soham Chowdhury
Soham Chowdhury

Reputation: 2275

Python returns unexpected answer

This is my program (a very simple one):

__author__="soham"
__date__ ="$Aug 12, 2012 4:28:51 PM$"

from math import sqrt

class Point:
    x = 0;
    y = 0;
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

    def get_dist(self, other):
        return sqrt(abs((self.x - other.x)^2 + (self.y - other.y)^2))

    def is_rect(self,other,another,yet_another):
        return (self.get_dist(other) == another.get_dist(yet_another)) and \
               (self.get_dist(another) == other.get_dist(yet_another)) and \
               (self.get_dist(yet_another) == other.get_dist(another))


a, b, c, d = Point(4,3),Point(4,9), Point(7,3), Point(7,9)

if a.is_rect(b,c,d):
    print "Rectangle."
else:
    print "No, not a rectangle!"

This returns no. A similar program written in Java returns the expected answer.

I'm very new to Python. Help!

Upvotes: 1

Views: 112

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799470

Exponentiation in Python is **, not ^.

^ in Python is actually the bitwise xor operator.

For exponentation, you can also do pow(x,y) instead of x**y.

Upvotes: 10

Related Questions