0

Started Leetcode today, First problem was to add two numbers (ik, very easy but still, link: https://leetcode.com/problems/add-two-integers/).

My answer is of five lines but the compiler says it has detected two errors on line 29 and 39

My answer:

num1 = 12
num2 = 5
sum = num1 + num2

print(sum)

Compiler error:

NameError: global name 'Solution' is not defined
    ret = Solution().sum(param_1, param_2)
Line 29 in _driver (Solution.py)
    _driver()
Line 39 in <module> (Solution.py)

Which makes absolutely no sense as there is no such line in my code

I have tried reloading, closing tab, etc. but to no effect. What am I doing wrong?

This photo contains the screenshot of the problem and my solution along with compile errors.

1
  • 1
    where is your class and def? try this : class Solution: def sum(self, num1: int, num2: int) -> int: return num1 + num2 Commented Jul 7, 2022 at 9:26

1 Answer 1

1

You need to include the function signature (see below):

class Solution:
    def sum(self, num1: int, num2: int) -> int:

Hence, your solution would be

class Solution:
    def sum(self, num1: int, num2: int) -> int:
        sum = num1 + num2
        return sum

This is because LeetCode runs your code within its judging system, and has testing code which you can't see (which is why it errors out at line 29 and 39).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.