Python Shell没有显示错误,但程序无法运行 [英] Python shell shows no error but program doesn't run

查看:294
本文介绍了Python Shell没有显示错误,但程序无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个程序来学习OOP的基础知识.当我从Python Shell中的IDLE运行该程序时,它没有显示任何错误,但也没有打印任何内容...我不确定如何找出我的错误.

I wrote this program to learn the basics of OOP. When I run this program from IDLE in the Python Shell it doesn't show any errors but also doesn't print anything... I'm not sure how to go about figuring out what my errors are.

这是我的代码:

class Shapes(object):
    def __init__(self, width, length):
        object.__init__(self)
        self.setWidth(width)
        self.setLength(length)

    def getWidth(self):
        return self.width

    def setWidth(self, width):
        if (width <= 0):
            width = 5
        else:
            width = self.width

    def getLength(self):
        return self.length

    def setLength(self, length):
        if (length <= 0):
            length = 10
        else:
            length = self.length

class Rectangle(Shapes):
    def __init__(self, area, perimeter):
        Shapes.__init__(self, length, width)

    def getArea(length, width):
        return length * width

    def getPerimeter(length, width):
        return (length * 2) + (width * 2)

    def getStats(self):
        print("Area: {}".format(self.getArea()))
        print("Perimeter: {}".format(self.getPerimeter()))
        print("Length: {}".format(self.getLength()))
        print("Width: {}".format(self.getWidth()))


def main():
    print("Rectangle a: ")
    a = Rectangle(5, 7)
    print("Area:          {}".format(a.area))
    print("Perimeter:     {}".format(a.perimeter))

    print( " ")
    print("Rectangle b: ")
    b = Rectangle()
    b.width = 10
    b.height = 20
    print(b.getStats())

如果要查看,这是Shell正在做的事情: http://imgur.com/DxyUZyY

Here's what the Shell is doing, if you want to look: http://imgur.com/DxyUZyY

我在做什么错,我该如何纠正?

What am I doing wrong and how can I correct this?

推荐答案

您永远不会调用主函数.与C语言不同,在C语言中,main函数将自动执行,而在python中,您必须显式调用它. main没有任何特殊意义,只是python中的另一个功能.

You never call the main function. Unlike in C, where main function would automatically execute, in python, you have to explicitly call it; main doesn't hold any special significance and is just another function in python.

因此,在代码末尾,写:

So at the end of your code, write:

main()

运行该命令后,您将看到诸如@Eric在其注释中指出的错误,等等.

Once you run that, you will see errors like @Eric pointed in his comments, and many more.

您当前的代码有很多问题,下面是其中的一些列表:

There are many things wrong with your current code, here is a list of a few of them:

  1. object.__init__(self)什么都不做
  2. self.setWidth(width)调用该方法,但是setWidth方法尝试将其设置为尚未声明的变量.

  1. object.__init__(self) doesn't do anything
  2. self.setWidth(width) calls the method, but the setWidth method tries to set to a variable that hasn't been declared yet.

  • 因此,这需要更正为

  • So this needs to be corrected to

def setWidth(self, width):
    if (width <= 0):
        width = 5
    else:
        width = width # and not self.width

setLength的情况也是如此.

  • 您需要查看super
  • lengthwidth此处未定义.
  • You need to look into super
  • length, width are not defined here.

这篇关于Python Shell没有显示错误,但程序无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆