+ 1

TypeError: '>' not supported between instances of 'NoneType' and 'int'

I'm trying to have written this code with the decorator that has to show these multiples (which are of 3 and 5) and their sum by typing a number of maximum range. Here is it: def multi(max_case): def three_and_five(): n = max_case() sum = 0 while n > 1: if n%3==0 or n%5==0: sum+=n print(n) elif n%3==0 and n%5==0: sum+=n print(n) n-=1 print('\n'+str(sum)) return three_and_five @multi def add_value(): n = int(input('Type a natural number: ')) print(n) add_value() Unfortunately there must be something wrong, because after running that and entering integer value I got this output: Line 5: TypeError: '>' not supported between instances of 'NoneType' and 'int' Does anyone have an idea why it's so and how to solve it?

28th Dec 2022, 9:15 PM
Nicole Shevrill
Nicole Shevrill - avatar
4 odpowiedzi
+ 4
Nicole Shevrill add_value is a void function that returns None. In the decorator you use n=max_case() when you run add_value, this becomes n = add_value() but add_value does not have a return statement, so it returns None. so: n = None You can't use this for your conditional statements. The decorator ends up comparing None to int. add a return n to your add_value function.
28th Dec 2022, 10:03 PM
Bob_Li
Bob_Li - avatar
+ 3
Nicole Shevrill This second example does not depend on the return value of the decorated function. You can pass it an empty function and it will still work. @decor def print_text(): pass print_text()
29th Dec 2022, 12:46 AM
Bob_Li
Bob_Li - avatar
+ 1
Thanks! Now it really works, but there is another thing that made me curious: def decor(put_it_here): def wrap(): print("============") w = put_it_here() print("============") return wrap @decor def print_text(): w = input() print(w) print_text() Why was return not needed as in the example above?
28th Dec 2022, 11:45 PM
Nicole Shevrill
Nicole Shevrill - avatar
+ 1
Thank you for your help, now it seems understandable to me.
29th Dec 2022, 8:03 PM
Nicole Shevrill
Nicole Shevrill - avatar