+ 1
military time - code debugging
Hi, Is anyone able to debug my code or at least find an input for which the function does not return the correct output? I did many ad hoc tests but I can't find the error. All tests pass but #4 (it's hidden). def myf(inp): l = len(inp) if inp[-2:] == "PM": if l == 7: return(str(eval(inp[0] + "+ 12")) + inp[1:-3]) else: if inp[:2] != "12": return(str(eval(inp[:2] + "+ 12")) + inp[2:-3]) else: return(inp[:-3]) else: if l == 7: return("0" + inp[:-3]) else: if inp[:2] != "12": return(inp[:-3]) else: return("00"+ inp[2:-3])
3 Respuestas
+ 4
It is giving error for numbers like 01:30 PM . Basically numbers prefixed with a zero .
As the number is prefixed with a zero , python interpreter doesn't allow evaluation of such number . It gives an error saying "octal number should be prefixed 0o".
Instead i replaced the following line ,
return(str(eval(inp[:2] + "+ 12")) + inp[2:-3]) with return(str(int(inp[:2]) + 12) + inp[2:-3]) .
And now it works fine.
I haven't tested it for other cases and i am not sure if numbers prefixed with zero are included in test cases as well .
+ 2
You are right Abhay, thank you. I did not know that error. Substituting that piece of code everything works. Anyway, I would just say that the problem seems not to admit numbers prefixed with zero because as sample inputs it uses 1:15 PM and 1:00 PM. In addition, military time does not use 0 prefixed numbers.
+ 1
Simone you are right but your code works for every other input i tried and it failed only on this type of input !!