有人能解释一下这个代码是怎么不正确的吗



我正在创建一段代码,该代码能够将24小时的时间转换为可以读取的时间(例如1分钟到午夜12点(。我几乎已经完成了代码,然而,有一些错误,因为当分钟数大于30时,我的代码会出错,比如2359将产生2359的时间是1分钟到晚上11点。这是我的代码

times = []
test_cases = int(input("How many test cases do you have?: "))
for _ in range(test_cases):
user_input = input(str("What is your time? "))
times.append(user_input)
for n in times:
first_half = n[:len(n) // 2]
second_half = int(n[len(n) // 2:])
if int(first_half) == 12:
first_half_2 = f"{12} noon"
elif int(first_half) == 00:
first_half_2 = f"{12} midnight"
elif int(first_half) > 12:
first_half_2 = f"{int(first_half) - 12}pm"
else:
first_half.strip("0")
first_half_2 = f"{int(first_half)}am"
if second_half == 00:
second_half_2 = ""
elif second_half == 1:
second_half_2 = "a minute to "
elif second_half == 15:
second_half_2 = "a quarter past "
elif second_half == 59:
second_half_2 = "a minute to "
elif (00 < second_half < 30) and second_half != 15:
second_half_2 = f"{second_half} minutes past "
elif second_half == 30:
second_half_2 = "half past "
elif (30 < second_half < 59) and second_half != 45:
second_half_2 = f"{60 - second_half} minutes to "
else:
second_half_2 = "a quarter to "
print(f"{n} is {second_half_2}{int(first_half_2) + 1}")

我重写了一些,但尽可能多地保留您的工作,使其易于理解!先看分钟,这样你就可以根据需要调整上半场。这不是最有效的方法,但应该相对容易理解。希望这能有所帮助:

while True:
time = input("Enter the time or -1 quit: ")
if time == "-1":
break

first_half = int(time[:len(time) // 2])
second_half = int(time[len(time) // 2:])
if first_half not in range(0,24) or second_half not in range(0,61):
print("Not an actual time...")
break

if second_half == 0:
second_half_2 = ""
elif second_half == 15:
second_half_2 = "a quarter past "
elif second_half in range(1, 30) and second_half!= 15:
second_half_2 = f"{second_half} minutes past "
elif second_half == 30:
second_half_2 = "half past "
elif second_half == 45:
second_half_2 = "a quarter to " 
first_half += 1
elif second_half == 59:
first_half += 1
second_half_2 = "a minute to " 
elif second_half in range(31, 60) and second_half != 45:
second_half_2 = f"{60 - second_half} minutes to "
first_half += 1
if first_half == 12:
first_half_2 = f"{12} noon"
elif first_half == 00 or first_half == 24:
first_half_2 = f"{12} midnight"
elif first_half in range(1,12):
first_half_2 = f"{first_half}am"
elif first_half in range(13,23):
first_half_2 = f"{first_half - 12}pm"


if len(str((first_half))) < 10:
print(f"{time} is {second_half_2}{int(first_half_2[:1])}{first_half_2[1:]}")
else:
print(f"{time} is {second_half_2}{int(first_half_2[:2])}{first_half_2[2:]}")

对于初学者来说,测试用例的输入是什么?它是用来测试你的代码,看看它是否有效吗?

我重写了你代码的第一部分,假设测试用例是不必要的,因为我觉得没有它,程序可以做你想做的事情。我希望这是一个公平的假设。这样做的目的是将军事时间划分为小时和分钟,这样就可以很容易地将它们分开处理,这似乎你已经想好了如何做到

user_input = str(input("What is your time?"))
def split_num(n): return [str(i) for i in str(n)]
def recombine(lst): return ''.join(lst)
split_time = split_num(user_input)
hour = recombine(split_time[0:2])
minute = recombine(split_time[2:5])

至于你遇到的问题,我试图修复它,但用户输入集合似乎还有其他更大的问题,这可能间接导致了这个问题。我认为现在它可能会自行解决,因为您将使用我上面写的代码更整齐地组织数据。你现在不需要任何for循环了。试试看,让我知道它是否有效!

此外,包含3个数字的elif语句的编写方式可能不起作用。我为你重写了它们:

elif (second_half > 0) and (second_half < 30) and (second_half != 15):
second_half_2 = f"{second_half} minutes past "
elif (second_half > 30) and (second_half < 59) and (second_half != 45):
second_half_2 = f"{60 - second_half} minutes to "

最新更新