我知道在S/O网络上有很多答案,但我在HackerRank python 3 shell中遇到了一个不寻常的错误,下面的代码在Jupyter notebook中运行良好
time1=input()
l=time1.split(':')
if 'PM' in l[2]:
l[0]=int(l[0])+12
l[2]=l[2].rstrip('PM')
elif 'AM' in l[2]:
l[2]=l[2].rstrip('AM')
if l[0]=='12':
l[0]="0"
time2=''
for i in range(2):
time2+=str(l[i])+':'
time2+=l[2]
print(time2)
这是challenge:
Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.
Note:
- 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.
Example
s = '12:01:00PM'
Return '12:01:00'.
s = '12:01:00AM'
Return '00:01:00'.
Function Description
Complete the
timeConversion
function in the editor below. It should return a new string representing the input time in 24 hour format.
timeConversion
has the following parameter(s):
- string s: a time in 12 hour format
Returns
- string: the time in 12 hour format
Input Format
A single string s that represents a time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM).
Constraints
- All input times are valid
Sample Input 0
07:05:45PM
Sample Output 0
19:05:45
我确实试过运行同样的案例,在H/R上出现错误,但在J/p笔记本上工作正常
另外,我知道这可能是我提出的一个超级基本的问题,但请原谅,我还是个新手:)
似乎有两个问题:
因此,改变这一点:
致:
改变这一点:
致:
有了这一点,它就会起作用。请注意,要求您编写
timeConversion
函数的主体,因此您的代码中不应该有硬编码的time1=
最终的代码可能是这样的:
相关问题 更多 >
编程相关推荐