簡體   English   中英

Python返回意外的答案

[英]Python returns unexpected answer

這是我的程序(一個非常簡單的程序):

__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!"

這將返回否。 用Java編寫的類似程序將返回預期的答案。

我是Python的新手 救命!

Python中的冪是** ,不是^

Python中的^實際上是按位xor運算符

對於指數,您也可以執行pow(x,y)而不是x**y

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM