简体   繁体   中英

Can someone please explain how this code is incorrect?

I am creating a piece of code that is able to transform 24 hour time to time that can be read (such as 1 minute to 12 midnight). I have nearly finished the code, however, there are a few bugs, as my code will make an error when the minutes is larger then 30, such as the time 2359 will produce 2359 is 1 minute to 11pm. This is my code

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}")

I rewrote some of the but tried to keep as much of your work as possible so it is easy to understand. It helps to look at the minutes first so you can adjust the first half as necessary, This is not the most efficient way. but it should be relatively easy to understand: Hopefully this helps:

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:]}")

For starters, what is the test case input for? Is it for testing your code to see if it works?

I rewrote the first part of your code with the assumption that the test case thing is not necessary because I feel that the program can do what you want it to without it. I hope this was a fair assumption. What this does is it splits up the military time up into hours and minutes so that they can easily be treated separately which it seems like you have already figured out how to do.

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])

As for the issue you were having, I tried fixing it but it seems that there are some other bigger problems with the user input collectio which may have indirectly led to the problem. I think it may sort itself out now that you'll have the data organized more neatly with the code I wrote above. You won't be needing any for loops now. Give that a try and let me know if it works!

Also, the elif statements with 3 numbers in them were written in a way that might not work. I rewrote them for you:

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 "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM