pset1 meal time but check fails


The problem require to convert user’s input to time and print meal time (i.e lunch time, dinner time e.t.c)
enter image description here

When I test my code with the time provided in the problem I get the expected output.

enter image description hereSadly, when I run check it fails.

Here is my code

def main():
    # ask user for input
    time_input = input("Time: ").strip().split(":")
    # convert time to decimal
    time = convert(time_input)

    # check time and print meal time
    if time >= 7.0 and time <= 8.0:
        print("breakfast time")
    elif time >= 12.0 and time <= 13.0:
        print("lunch time")

    elif time >= 18.0 and time <= 19.0:
        print("dinner time")

def convert(t):
    # convert string to float(hour)
    hour = float(t[0])
    # convert string to float(minute)
    minute = float(t[1]) / 60
    return hour + minute

if __name__ == "__main__":

Below is the error I get after running check.

enter image description here I don’t know why I am getting this results from check

Asked By: Balup



I would structure program and divide the problem into following testable functions:

  1. read user-input (day-time like 18:15) as string
  2. convert time-string into hours as float (e.g. 18.25 for 18:15)
  3. return meal-time name for given time (or hours float)
# 1. Example: return a valid ISO-time in format HH:MM or show error and ask again
def ask_for_time():
    # your code using input, a prompt and maybe validation
    return '18:15'

# 2. Example: from local ISO-time format `18:15` to hours incl. fractions `18.25` 
def parse_to_hours(iso_time):
    # your code using strip, split, type-conversion and calculations
    # or use datetime.strptime(date_string, format)
    return 18.25

# 3. Example: 'dinner time' for '18:15' 
def mealtime_for(iso_time):
    hours = parse_to_hours(iso_time)
    # conditional returns
    if  hours >= 18 and hours <= 19:
        return 'dinner time'
    # default
    return 'outside meal-time'


This way each function can be tested. For example using doctest:

def meal_time_for(hours):
    """a doctest in a docstring
    >>> meal_time_for(1)
    outside meal-time
    >>> meal_time_for(18)
    dinner time
    >>> meal_time_for(12.75)
    lunch time
    # implementation starts here


If all test-cases have been passed, then the assembled script should work correctly.

if __name__ == "__main__":
    iso_time = ask_for_time()
    meal_time = meal_time_for(iso_time)
    print(f"{iso_time} is {meal_time}")

See also:

Answered By: hc_dev
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.