pythonrelational-operators

python relational inclusive operators not inludind start and end values


The goal of the function is to output a value in a given range. Including the start and end value if it is entered as input. The function only outputs the expected result for values at the start and between the range.

def main():
    #assume user input will be formatted in 24-hour time as #:## or ##:## 
    time = input("What is the 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(time):
    h, m = time.split(":")
    time = float(((float(h) * (60)) + float(m)) / 60)
    return time

if __name__ == "__main__":
    main()

Solution

  • You need first to call you convert method, and change the conditions to avoid exception comparing string with float

    def main():
        time = convert(input("What is the 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(time):
        h, m = time.split(":")
        time = float((int(h) * (60) + int(m)) / 60)
        return time
    
    if __name__ == "__main__":
        main()