简体   繁体   English

有没有更好的方法来解决这个问题,创建一个类来为计算器调用不同的函数?(Python 3.3)

[英]Is there a better way to go about this, creating a class to call different functions for a calculator?(Python 3.3)

I am really new at this whole programming thing. 在整个编程方面,我真的是新手。 The below code works good, I was just wondering if there are other ways to implement this. 下面的代码很好用,我只是想知道是否还有其他方法可以实现此目的。

Should my init function be that complex or should I call another function with it to can call my other functions? 我的init函数应该那么复杂,还是应该用它调用另一个函数才能调用我的其他函数?

class calculator():

    def __init__(self):
        while True:
            self.ask = str(input("What would you like to calculate? Degrees, Trapezoids, or Circles?"))
            if self.ask == "degrees":
                self.f_to_c_calc()
            elif self.ask == "trapezoid":
                self.calc_trapeziod_area()
            elif self.ask == "circle":
                self.cal_circle()
            else:
                break

    def f_to_c_calc(self):
        temp = int(input("How warm is it out?"))
        f_or_c = str(input("Is it Celsius or Fahrenheit?"))
        if f_or_c == "c" or f_or_c == "C":
            updatedtemp = float(temp * 9 / 5 + 32)
            print (updatedtemp, "°F")
        elif f_or_c == "f" or f_or_c == "F":
            updatedtemp = float((temp - 32) * 5 / 9)
            print (updatedtemp, "°C")

    def calc_trapeziod_area(self):
        height = float(input("What is the height of the trapezoid?"))
        length1 = float(input("What is the length of the top base?"))
        length2 = float(input("What is the length of the bottom base?"))
        formula = float((1 / 2 * (length1 + length2)) * height)
        print ("Your Trapezoids area is:",(formula))

    def cal_circle(self):
        pi = float(245850922 / 78256779)
        rad_or_diam = str(input("Is the number the Diameter or Radius?"))
        if rad_or_diam == "r" or rad_or_diam == "radius":
            radius = float(input("What is the radius of the circle?"))
            area_circ = float(pi * radius ** 2)
            print (area_circ)
        elif rad_or_diam == "d" or rad_or_diam == "diameter":
            diameter = float(input("What is the Diameter of the circle?"))
            area_circ = float(pi * diameter)
            print (area_circ)

if __name__ == "__main__":
    caculate = calculator()    

In general, "this works, but are there other ways to do this?" 通常,“这有效,但是还有其他方法可以做到这一点吗?” is not a good format for this site. 这不是此网站的好格式。 However, here's a partial answer: classes are for binding state to function, and not for organizing code. 但是,这是部分答案:类用于将状态绑定到功能,而不是用于组织代码。 (Opinions differ on this, which is one reason this is not a good question format for this site, but this is mine -- for more info on this theory, see Stop Writing Classes .) By and large, there is no utility to using a class to organize your code this way, because the functionality of your program is procedural. (对此有不同的意见,这是该网站不是很好的问题格式的一个原因,但这是我的-有关此理论的更多信息,请参见停止编写类 。)总的来说,没有使用实用程序一个以这种方式组织代码的类,因为程序的功能是过程性的。 If you were to break off all of these methods and turn them into functions, you could probably end up with less complex code that does the same thing. 如果要分解所有这些方法并将其转换为函数,则可能最终会得到执行相同功能的不太复杂的代码。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM