CS50 meal problem, Python 3, Error on decimal value return from convert() function

36 Views Asked by At

So we should prompt the user to input time as #:## a.m./p.m., the function convert should convert the inputted time to a float, return the float value and use print adequate text.

I'm receiving an Error stating that the convert function isn't returning a decimal value, but the terminal is passing all the tests, when I type in manually.

What's the issue?

def main():

    text = input("time here: ")
    time = convert(text) # call the function convert() and assign it to time variable, different from the one in the function
    print(time, type(time)) # check if the function returns a float value

    if 7 <= time <= 8: # simple logic for defining time
        print("breakfast time")
    elif 12 <= time <= 13:
        print("lunch time")
    elif 18 <= time <= 19:
        print("dinner time")
    elif time >= 24: # ensure that you can't type in number > 24
        print("Not a valid time point.")
    else:
        None # print None (null) for other cases

def convert(time):

    hour, minute, period = time.replace(":", " ").split(" ") # unpack the list with 3 arguments (3rd arugment is period = am/pm) and define floats
    hour = float(hour)
    minute = float(minute) / 60
    n_time = hour + minute # assign to a new variable n_time which should return float

    return round(n_time, 2) # return float, rounded to 2 decimals


if __name__ == "__main__":
    main()
1

There are 1 best solutions below

3
DinoCoderSaurus On

As per the spec:

Assume that the user’s input will be formatted in 24-hour time as #:## or ##:##

This line will error with that input:
hour, minute, period = time.replace(":", " ").split(" ") # unpack the list with 3 arguments (3rd arugment is period = am/pm) and define floats
errors

The Challenge says [emphasis added]:

If up for a challenge, optionally add support for 12-hour times, allowing the user to input times in these formats too:

#:## a.m. and ##:## a.m.
#:## p.m. and ##:## p.m.

The Challenge must handle either format.