如何解决有人可以解释一下这段代码是如何不正确的吗?
我正在创建一段代码,可以将 24 小时时间转换为可以读取的时间(例如 1 分钟到午夜 12 点)。我已经快写完了代码,但是有一些bug,因为当分钟大于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 "
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。