0

I am trying to learn how to use decorators by using the following code but I don't know why it's showing a type error.

#WAP to calculate Q = [(2*C*D)/H]^(1/2)
c = int(input("Enter a number : "))
d = int(input("Enter a number : "))
h = int(input("Enter a number : "))
def smart(func):
    def inner(h):
        if h == 0:
            q = "It is not possible."
            return
        return func
    return inner(h)
@smart
def calculate(c,d,h):
    q = ((2*c*d)/h)**(1/2)
    print(q)
calculate(c,d,h)

OK so i have solved the issue but I still don't know why it works could someone explain.

#WAP to calculate Q = [(2*C*D)/H]^(1/2)
c = int(input("Enter a number : "))
d = int(input("Enter a number : "))
h = int(input("Enter a number : "))
def smart(func):
    def inner(c,d,h):
        if h == 0:
            print("It is not possible.")
        else:
            return func(c,d,h)
    return inner
@smart
def calculate(c,d,h):
    q = ((2*c*d)/h)**(1/2)
    print(q)
calculate(c,d,h)
  • 1
    Share the error please – azro May 28 '20 at 08:41
  • Does this answer your question? [Python decorators in classes](https://stackoverflow.com/questions/1263451/python-decorators-in-classes) – Pasindu Gamarachchi May 28 '20 at 08:42
  • 1
    for starters, function that you are trying to use as a decorator, `smart`, *doesn't return anything*. It needs to return a callable if you want it to work. – juanpa.arrivillaga May 28 '20 at 08:44
  • I still don't fully understand. Could you please edit the code and explain. #WAP to calculate Q = [(2*C*D)/H] C = int(input("Enter a number : ")) D = int(input("Enter a number : ")) H = int(input("Enter a number : ")) def smart(func): def inner(H): if H == 0: print("It is not possible.") return return func @smart def calculate(C,D,H): return (((2*C*D)/H)**(1/2)) Q = calculate(C,D,H) print(Q) Ok so I edited it in the above way but it still throws an error when H=0. – Ayush Bajracharya May 28 '20 at 09:11
  • Please [edit] your question instead of adding comments. Code is unreadable in comments. See [ask]. – Robert May 29 '20 at 16:21
  • I have edited the question, do ignore my previous comment. – Ayush Bajracharya May 30 '20 at 07:10

0 Answers0