Converting to military time works in Jupyter Notebook but not in HackerRank

Viewed 36

My code works for all sample input hours of the day in Jupyter notebook but I get error message in HackerRank:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'(it doesn't tell me which line)

input : string of AM/PM format output: string of military time

def timeConversion(s):
    if int(s[:2]) < 12 and s[8] == "P":
        time = int(s[:2]) + 12
        print(str(time) + s[2:8])
    elif int(s[:2]) == 12 and s[8] == "A":
        time2 = int(s[:2]) - 12
        print(str(time2) + s[2:8])    
        
    else:
        print(str(s[0:8]))
1 Answers

Note that timeConversion, in its current form, prints the output and returns None (since you didn't include a return statement). I suspect that the HackerRank problem is set up to work with a timeConversion function that returns a string. With that said, I would try replacing your function with the following.

def timeConversion(s):
    if int(s[:2]) < 12 and s[8] == "P":
        time = int(s[:2]) + 12
        return str(time) + s[2:8]
    elif int(s[:2]) == 12 and s[8] == "A":
        time2 = int(s[:2]) - 12
        return str(time2) + s[2:8]
        
    else:
        return str(s[0:8])
Related