-1

I've wrote my answer code and tried to run it on the LeetCode playgroundhttps://leetcode.com/playground/.

In the part of

        print(check_num)
        print(check_num[len(check_num)-1])

it was revealed that the program can receive the input and its last character.

class Solution:
    def reverse(self, x: int) -> int:
        check_num = str(x)
        flag = 0
        if(check_num[0] == '-'):
            check_num = check_num[1:]
            flag = 0

        print(check_num)
        print(check_num[len(check_num)-1])

        else if (check_num[len(check_num)-1] == '0'):
            check_num = check_num[:len(check_num)-1]

        #reverse
        storage = ['a'] * len(check_num) - 1
        for i in range(len(check_num)):
            num = -i
            storage[i] = check_num[num]

        if(flag == 1):
            storage.insert(0, '-')

        #turn to string
        oneLinerString=""
        for x in storage:
            oneLinerString += x

        return int(oneLinerString)

def main():
    import sys
    import io
    def readlines():
        for line in io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8'):
            yield line.strip('\n')

    lines = readlines()
    while True:
        try:
            line = next(lines)
            x = int(line);

            ret = Solution().reverse(x)

            out = str(ret);
            print(out)
        except StopIteration:
            break

if __name__ == '__main__':
    main()

How should I fix my current code?

Finished in N/A
Line 12: SyntaxError: invalid syntax
NPP
  • 299
  • 2
  • 4
  • 11
  • 1
    `else if` is not valid Python syntax. Did you mean `elif`? Also keep in mind that an `elif` block can only appear immediately after an `if` or `elif` block, so the indentation you've got now won't work. – Kevin Sep 12 '19 at 13:49

1 Answers1

2

As you can see the error message, there's a syntax error on line 12. else if is not valid syntax in Python. Change it to elif.

Suraj
  • 602
  • 3
  • 16