0

Help me to fix the error in it

n = input() y = n.split(" ") for i in y: if "AM" in i: print(y[0]) if "PM" in i: n[0] = n[0]+12 print(n) You want to convert the time from a 12 hour clock to a 24 hour clock. If you are given the time on a 12 hour clock, you should output the time as it would appear on a 24 hour clock. Task: Determine if the time you are given is AM or PM, then convert that value to the way that it would appear on a 24 hour clock. Input Format: A string that includes the time, then a space and the indicator for AM or PM. Output Format: A string that includes the time in a 24 hour format (XX:XX) Sample Input: 1:15 PM Sample Output: 13:15

16th Jul 2021, 2:59 AM
Shahir
Shahir - avatar
1 Odpowiedź
+ 2
Firstly, the for loop is unnecessary for this and only serves to complicate things. Your second problem is you’re attempted to add an integer to a string. To solve this it may be wiser to split at the “:” rather than the space so we can seperate the hours from the minutes. For PM, we can convert the hours string into an integer to perform the addition. Then we convert it back to a string and concatinate it with a “:” and the first part of the y[1] value, which is the minutes. n = input() y = n.split(":") if "AM" in y[1]: print(y[0] + ":" + y[1][:2]) else: y[0] = int(y[0])+12 print(str(y[0]) + ":" + y[1][:2])
16th Jul 2021, 3:39 AM
Kylie
Kylie - avatar