Home > Mobile >  How do I write military time 600 - 1100 in python (without the leading zero)?
How do I write military time 600 - 1100 in python (without the leading zero)?

Time:09-26

This program tells you if a retaurant is open or closed in milatry time

print("Welcome to the Terrier Cafe! Enter the current time below and this program suggets a 
meal and a drink...")
print()
time = int(input("Enter  the current time of day useing a 24-hour clock (do NOT use a leading 
zero): "))
if time == 600-1100:
    print("We are open!")
    print("We are serving breakfast.")
    print("We suggest a cup of coffe and eggs and bacon to start your day!")
    print("Thanks for choosing our restaurant.")
elif time == 1100-1600:
    print("We are open!")
    print("We are serving lunch.")
    print("We suggest a glass of soda and a crispy grilled cheese sandwich.")
    print("Thanks for choosing our restaurant.")

This line has the error and the ones on top:

elif time == 1600-2300:
    print("We are open!")
    print("We are open!")
    print("We are serving dinner.")
    print("We reccomend a cold glass of water and a plate of rice and chicken.")
else: 
    print("Sorry we re closed!")
    print("Thank you for choosing our restaurant.")

CodePudding user response:

600-1000 evaluates to -400 which is False unless you happen to enter that value for time. You want to use conditions like this:

if 600 <= time < 1100:
   ...
elif 1100 <= time < 1600:
   ...
elif 1600 <= time < 2300:
  ...
else:
  print("Closed")

As the schedule is consecutive, and evaluated in order you can also write it as:

if time < 600 or time >= 2300:
   print("Closed")
elif time < 1100:
   ...
elif time < 1600:
   ...
else:
   ...

Or you can drive it via data (hours) like this:

def message(time):
    hours = [
       (600, "Closed"),
       (1100, "Breakfast"),
       (1600, "Lunch"),
       (2300, "Dinner")
    ]
    closed = hours[0][1]

    for (hour, message) in hours:
        if time < hour:
            return message
    return closed

print(message(time))
  • Related