class Square:
def empty():
return
print "What Is The Length And Width Of Your Square"
total = raw_input()
def _init_(self, size):
self.size = total
def getArea(self):
area = self.size * self.size
return area
mySquare = Square()
Square.getArea()
我已经尝试了一切,但我一直都会遇到错误。对不起,我是Python的新手,但请有人帮帮我吗?提前谢谢。
答案 0 :(得分:1)
您的代码有几处错误,而不仅仅是Paco提到的错误。
试试这个:
class Square:
global total
def empty():
return
print "What Is The Length And Width Of Your Square"
total = raw_input()
def __init__(self):
self.size = int(total)
def getArea(self):
area = self.size * self.size
return area
mySquare = Square()
print mySquare.getArea()
答案 1 :(得分:1)
不应该考虑让用户输入设置广场大小的类。更好的方法是将输入移到类外,并简单地将用户输入的大小作为Square构造函数的参数传递(您已经这样做了,但不要使用)。这样,你可以重用这个类。
class Square:
def empty(self):
return
def __init__(self, size):
self.size = size # <---
def getArea(self):
area = self.size * self.size
return area
size = raw_input("What Is The Length And Width Of Your Square? ") # <---
mySquare = Square(size)
mySquare.getArea()
答案 2 :(得分:0)
调用实例方法getArea:
mySquare.getArea()
而不是
Square.getArea()
答案 3 :(得分:0)
尝试
mySquare = Square(4)
mySquare.getArea()
而不是你写的东西,看看它是否有帮助。