简体   繁体   English

我试图了解类和函数,但似乎无法弄清楚我的代码出了什么问题

[英]I am trying to understand class and function and can't seem to figure out what is wrong with my code

Calculating the area of a triangle 计算三角形的面积

class area:

    def traingle(self,height,length):
        self.height=height
        self.length=length

    def calculate(self,maths):
        self.maths= (self.height)*(self.length)*(0.5)

    def answer(self):
        print 'Hello, the aswer is %i'%self.maths

first= area()

first.traingle(4,5)

first.calculate

print first.answer

What about this? 那这个呢?

import math


class Triangle:

    def __init__(self, height, length):
        self.height = height
        self.length = length

    def calculate(self):
        return (self.height) * (self.length) * (0.5)

    def answer(self):
        print 'Hello, the aswer is %.2f' % self.calculate()

first = Triangle(4, 5)
first.answer()

Remember, to call a method you need to use parenthesis, when you're doing first.answer you're not executing your method, instead you should be doing first.answer() 请记住, first.answer调用方法,则需要先使用括号。在执行first.answer您不必执行方法,而应该先执行操作first.answer()

Another different solution for this type of problem could be something like this: 此类问题的另一种不同解决方案可能是这样的:

import math


class Triangle:

    def __init__(self, height, length):
        self.height = height
        self.length = length

    def area(self):
        return (self.height) * (self.length) * (0.5)


class Quad:

    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height


for index, obj in enumerate([Triangle(4, 5), Quad(2, 3)]):
    print 'Area for {0} {1} = {2:.2f}'.format(obj.__class__, index, obj.area())

In any case, make sure you go through some of the available python tutorials out there in order to understand all the concepts first ;-) 无论如何,请确保您已阅读一些可用的python教程 ,以便首先理解所有概念;-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想不通这个 function 有什么问题? - I can't figure out what is wrong with this function? Code Jam 问题:Parent Partnering 返回 - 尽管尝试了许多不同类型的测试用例,但似乎无法找出问题所在 - Code Jam Question: Parent Partnering Returns - Can't seem to figure out what's wrong despite trying many different kinds of test cases 我想弄清楚为什么我的字谜 function 弄错了 output - I am trying to figure out why my anagram function is getting the wrong output 试图找出有关列表的代码出了什么问题? - trying to figure out what's wrong with my code regarding lists? 我的代码中第 7 行的语法有什么问题? 它说我的打印语句有无效的语法,我无法弄清楚? - What is wrong with the syntax with line 7 in my code? It says my print statement has invalid syntax and I can't figure it out? 无法弄清楚代码有什么问题。 尝试 string.find() 的替代方法 - Can't figure out what is wrong with the code. Trying an alternative for string.find() 我无法理解我在 Kaggle 比赛中的训练和测试数据做错了什么 - I can't understand what I am doing wrong with my training and my test data for a competition on Kaggle 我正在尝试使用模块 simplegui 在 CodeSkulptor 中制作一个带有两个输入字段的计算器。 我不知道我做错了什么 - I'm trying to make a calculator with two input fields in CodeSkulptor using the module simplegui. I can't figure out what I am doing wrong 25 Locker问题-无法弄清楚我的代码出了什么问题 - 25 Locker Problem - can't figure out what's wrong with my code 无法弄清楚我的方法出了什么问题 - Can't figure out what's wrong with my method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM