I’m solving cs50 python week 1 ‘meal’ problem. I got a error “convert successfully returns decimal hours expected “7.5”, not ‘Errorn'” and another one is “can’t check until a frown turns upside down”. But the code is working properly in my system.
def main():
print("If you use 12-hour time use '##:##a.m.' OR '##:##p.m.' format'")
time_input = input("What time is it? ").strip().split(":")
# convert time to decimal
time = convert(time_input)
# check time and print meal time
if 7.0<= time <=8.0:
print("breakfast time")
elif 12.0<= time <=13.0:
print("lunch time")
elif 18.0<= time <=19.0:
print("dinner time")
def convert(t):
#Condition for 12pm time
if t[0] == "12" and t[1].find("p.m.") != -1:
t[1] = t[1].replace("p.m.", "") #Remove 'p.m.'
#condtion of 12am time
if t[0] == "12" and t[1].find("a.m.") != -1:
t[0] = "00" #If time is 12a.m. convert it to 0:0 time
#Removing 'am' from user input
if t[1].find("a.m.") != -1:
t[1] = t[1].replace("a.m.", "")
#Removing 'pm' from user input and adding 12 to make it 24 hour time.
if t[1].find("p.m.") != -1:
t[1] = t[1].replace("p.m.", "")
t[0] = float(t[0])+12
# 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__":
main()
1