Created
August 1, 2022 10:08
-
-
Save Omi98/00ee46c16edaa2ceeb322afddde8b0f0 to your computer and use it in GitHub Desktop.
meal.py code for cs50p - pset1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
time = input("What time is it? ") | |
# call convert func | |
meal_time = convert(time) | |
# returns and assigns meal_time | |
# print(meal_time) | |
# defining ranges for | |
# - breakfast time | |
# - lunch time | |
# - dinner time | |
# range(start, stop) - inclusive | |
# !!! - range() cannot be used with float() --- !!! | |
''' | |
breakfast = range(7, 8) | |
lunch = range(12, 13) | |
dinner = range(18, 19) | |
# defining if-conditionals | |
if meal_time in breakfast: | |
print("breakfast time") | |
elif meal_time in lunch: | |
print("lunch_time") | |
elif meal_time in dinner: | |
print("dinner time") | |
else: | |
print() | |
''' | |
if meal_time >= 7.0 and meal_time < 8.0: | |
print("breakfast time") | |
elif meal_time >= 12.0 and meal_time < 13.0: | |
print("lunch time") | |
elif meal_time >= 18.0 and meal_time < 19.0: | |
print("dinner time") | |
else: # overriding endline parameter of print() | |
print("", end='') | |
def convert(time): | |
# split time str to hrs and mins | |
# hrs = hours | |
# mins = minutes | |
hrs, mins = time.split(":") | |
# convert hrs and mins to float | |
# float_hours | |
# float_mins | |
f_hrs = float(hrs) | |
f_mins = float(mins) | |
# 60 minutes in 1 hour | |
# to get float for mins | |
# divide mins value with 60 | |
# c_mins = converted_mins | |
c_mins = f_mins / 60.0 | |
# add hours and minutes | |
# ans will be float | |
meal_time = f_hrs + c_mins | |
return meal_time | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment