1

I am trying to take two values as parameters and return True if its value is equal to 10 and false if it isn't. The values are strictly int. Here is the code

    class Solution:
    def twomakes10(self, no1, no2):

        if sum(no1, no2) == 10:
            return True
        else:
            return False


if __name__ == "__main__":
    p = Solution()
    n1 = 9
    n2 = 1
    print(p.twomakes10(n1, n2))
5
  • 1
    (1) Fix the indentation of the code. (2) Show the full traceback of the error as properly formatted text in the question. Commented Nov 23, 2022 at 16:42
  • The sum() function applies to sequences - lists, tuples, etc. Simply adding two numbers is written no1 + no2. Commented Nov 23, 2022 at 16:43
  • The first parameter of sum() should be an iterable. See doc. You can simply do if no1 + no2 == 10: Commented Nov 23, 2022 at 16:43
  • 1
    There is no reason to use if ... else to transform a boolean into a boolean. Your 4-line definition for twomakes10 can be replaced by the single line return no1+no2 == 10 Commented Nov 23, 2022 at 16:51
  • 2
    To be clear: the question is "how do I add two numbers together in Python?" ? Commented Nov 23, 2022 at 16:54

2 Answers 2

1

sum function, gets an iterable value as input, you can try:

sum([no1,no2])
Sign up to request clarification or add additional context in comments.

2 Comments

But why use sum([no1,no2]) rather than no1 + no2?
You are right. I just pointed out the behavior of sum, in the case of generalizing the make10 function, to receive unlimited inputs, using sum may be effective.
0

sum is expecting iterable .. so make it happy. See below

class Solution:

    def twomakes10(self, no1, no2):
        return sum([no1, no2])


if __name__ == "__main__":
    p = Solution()
    n1 = 9
    n2 = 1
    print(p.twomakes10(n1, n2))

1 Comment

not anymore ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.