【问题标题】:Process finished with exit code 0进程以退出代码 0 结束
【发布时间】:2017-01-19 07:30:50
【问题描述】:

我有这个:

import  math
class Point:
    def move(self, x, y):
        self.x = x
        self.y = y
def reset(self):
    self.move(0, 0)
def calculate_distance(self, other_point):
    return math.sqrt(
(self.x - other_point.x)**2 +(self.y - other_point.y)**2)
# how to use it:
    point1 = Point()
    point2 = Point()
    point1.reset()
    point2.move(5,0)
    print(point2.calculate_distance(point1))
    assert (point2.calculate_distance(point1) ==   point1.calculate_distance(point2))
    point1.move(3,4)
    print(point1.calculate_distance(point2))
    print(point1.calculate_distance(point1))

所以我希望它会像这样打印:

5.0
4.472135955
0.0

但在控制台中的 pycharm 中,它只打印以下内容:

Process finished with exit code 0

在哪里可以看到输出?

为了清楚起见,我还添加了一个附件。

谢谢

【问题讨论】:

  • 在我看来,鉴于最后 9 行代码的缩进,甚至没有执行任何代码。程序在没有真正执行任何代码的情况下完成。修复缩进,它至少应该正确执行。现在,最后 9 行代码属于 calculate_distance 函数。

标签: python


【解决方案1】:

有一个名为“python 控制台”的窗口。你的脚本的输出应该在那里......

【讨论】:

【解决方案2】:

问题是resetcalculate_distance 函数不在Point 类中,因为缺少缩进。

试试这个。现在函数是 Point 类的方法,一切正常:

import  math

class Point:

    def move(self, x, y):
        self.x = x
        self.y = y

    def reset(self):
        self.move(0, 0)

    def calculate_distance(self, other_point):
        return math.sqrt((self.x - other_point.x)**2 +(self.y - other_point.y)**2)


# how to use it:
point1 = Point()
point2 = Point()
point1.reset()
point2.move(5,0)
print(point2.calculate_distance(point1))
assert (point2.calculate_distance(point1) ==   point1.calculate_distance(point2))
point1.move(3,4)
print(point1.calculate_distance(point2))
print(point1.calculate_distance(point1)) 

【讨论】:

    猜你喜欢
    • 2015-05-23
    • 2018-05-29
    • 2013-04-19
    • 1970-01-01
    • 2021-02-20
    • 2014-01-04
    • 2017-11-03
    • 2020-01-04
    • 1970-01-01
    相关资源
    最近更新 更多