Help with the code

This code seems to working fine in jupyter notebook but isn’t passing any of the test cases.