+ 1

Code Coach Military Time

Follwing is the code that I have written for the military time problem in the code coach section. When I ran my code in IDLE it gives me the desired output but when I run it in this platform it gives no output. Please help me fix this out. hour = input() minutes = input() duration = input() if int(hour) == 12 and duration == 'AM': hour = "00" elif int(hour) == 12 and duration == 'PM': hour = 12 if duration == 'PM' and int(hour) != 12: hour = 12 + hour time24 = str(hour) + ":" + str(minutes) print(time24)

29th Jun 2020, 8:22 PM
Neeraj Jain
Neeraj Jain - avatar
7 Answers
+ 2
There is only one input in code coach so you should have only one input() statement. That will have all the info needed for the task, you just need to split the input up into hour, minutes, etc.
29th Jun 2020, 8:45 PM
Russ
Russ - avatar
+ 2
Neeraj Jain If input is 12:xx PM, your code will output 24:xx because the "PM" is checked before the hours = 12 and am_pm = "PM". Try switching the second elif and the if conditionals around.
30th Jun 2020, 8:45 AM
Russ
Russ - avatar
+ 2
Neeraj Jain I think the issue now is that, when the hour is less than 10, you need a preceding 0 to make the hour up to two digits. Output must be of format HH:MM. If the hour is 1, you must output 01:xx for it to be correct.
30th Jun 2020, 4:07 PM
Russ
Russ - avatar
+ 1
Russ I have made the changes in my code. When I run it using IDLE I didn't get the error mentioned by you in the previous comment but in this platform still 2 test cases are not passed out of 5. Those are hidden test cases.Please help me out with this. Here is the modified code: time = input("") split_time = time.split(" ") split_hour_minutes = split_time[0].split(":") am_or_pm = split_time[1] hours = split_hour_minutes[0] minutes = split_hour_minutes[1] if am_or_pm == 'PM' and int(hours) == 12: hours = 12 elif am_or_pm == 'PM' and int(hours) != 12: hours = int(hours) + 12 elif int(hours) == 12 and am_or_pm == 'AM': hours = "00" time24 = str(hours) + ":" + str(minutes) print(time24)
30th Jun 2020, 4:04 PM
Neeraj Jain
Neeraj Jain - avatar
+ 1
Thank you so much Russ đŸ‘đŸ‘đŸ‘đŸ’ŻđŸ’„đŸ˜Ź
30th Jun 2020, 4:16 PM
Neeraj Jain
Neeraj Jain - avatar
+ 1
Congrats! 🎉🎉👍
30th Jun 2020, 4:20 PM
Russ
Russ - avatar
0
Russ I have made the necessary changes in my code still 2 test cases did not pass out of 5 test cases Please help me out with this. time = input("") split_time = time.split(" ") split_hour_minutes = split_time[0].split(":") am_or_pm = split_time[1] hours = split_hour_minutes[0] minutes = split_hour_minutes[1] if am_or_pm == 'PM': hours = int(hours) + 12 elif int(hours) == 12 and am_or_pm == 'AM': hours = '00' elif int(hours) == 12 and am_or_pm == 'PM': hours = 12 time24 = str(hours) + ":" + str(minutes) print(time24)
30th Jun 2020, 8:00 AM
Neeraj Jain
Neeraj Jain - avatar