'Why is this showing type error in python?

import math
import os
import random
import re
import sys



def timeConversion(s):
    if "P" in s:
        x = re.split("\W", s)
        y = int(x[0]) + 12
        z = str(y)
        a = re.sub("^\d\d", z, s)
        b = re.sub("[a-zA-Z]", "", a)
        print(b)
    else:
        b = re.sub("[a-zA-z]", "", s)
        print(b)

if _name_ == '_main_':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = raw_input()

    result = timeConversion(s)

    fptr.write(result, + '\n')

    fptr.close()

This is the code. So my question is.

Traceback (most recent call last):
  File "Solution.py", line 35, in <module>
TypeError: unsupported operand type for unary pos: 'str'

why is this error and how to mitigate it? Any suggestions anyone?



Solution 1:[1]

You have a problem with this line

 fptr.write(result, + '\n')

I assume its a typo, the comma came in between and ideally it must be

fptr.write(result + '\n')

The symbol '+' was applied like a sign operator for \n because of the comma (like you say -10 or +10) which is invalid for a string. So the error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kris