#🔒 I can't get this CS50 problem set (PS1 Meal Time) to pass the tests. (Even though it should I thin

54 messages · Page 1 of 1 (latest)

astral lion
#

These are the instructions:

In meal.py, implement a program that prompts the user for a time and outputs whether it’s breakfast time, lunch time, or dinner time. If it’s not time for a meal, don’t output anything at all. Assume that the user’s input will be formatted in 24-hour time as #:## or ##:##. And assume that each meal’s time range is inclusive. For instance, whether it’s 7:00, 7:01, 7:59, or 8:00, or anytime in between, it’s time for breakfast.

Structure your program per the below, wherein convert is a function (that can be called by main) that converts time, a str in 24-hour format, to the corresponding number of hours as a float. For instance, given a time like "7:30" (i.e., 7 hours and 30 minutes), convert should return 7.5 (i.e., 7.5 hours).

from:
https://cs50.harvard.edu/python/2022/psets/1/meal/

This is my code: https://paste.pythondiscord.com/6ONQ

And here are the cs50 check50 results: https://paste.pythondiscord.com/X2FQ

fickle groveBOT
#

@astral lion

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

astral lion
#

The expected is to pass the 7:30/7.5 test at least, since when I input 7:30 it prints 7.5.

What actually happens is that it supposedly returns Error\n instead of 7.5.

lilac lynx
#

!e

def convert(time):
    hour, mins = time.split(":")
    flhour, flmins = float(hour), float(mins) / 60
    result = flhour + flmins
##    print(result) --- debugging: input 7:30 result=7.5
    return result

print(convert("7:30"))
print(convert("7:45"))
print(convert("7:50"))
print(convert("7:55"))
print(convert("7:60"))  # probably they want that here an error is raised?
print(convert("7:65"))  # here too
fickle groveBOT
astral lion
#

sorry what?

#

my bad I'm just getting into coding

#

what did I do wrong?

lilac lynx
#

i do not know if you did something wrong. i could imagine that wrong times like 7:65 should be intercepted

#
def main():
    dtime = convert()
    if float(7) <= dtime <= float(8):
        print("breakfast time")
    elif float(12) <= dtime <= float(13):
        print("lunch time")
    elif float(18) <= dtime <= float(19):
        print("dinner time")

you do not need to convert the ints to floats to be able to compare them with floats

astral lion
#

so just put 7.0, 8.0 etc

lilac lynx
#

no just 7, 8, etc

#

but what excatly does not work? your programme works as required but you do not pass the test?

astral lion
#

yeah

#

thats what's wird

#

weird

lilac lynx
#

does the test provide an error message?

#

@astral lion ?

lilac lynx
#

ok cringe. probably someone who also did that course can help. but your code is correct as we can see above.

astral lion
#

i have an idea

#

wait

lilac lynx
#

my only guess would be

flhour, flmins = float(hour.strip()), float(mins.strip()) / 60

if they add randomly some whitespace to the input

astral lion
#

wouldnt it be better to add like input("..").strip()

lilac lynx
#

if they do "7 : 30" it would not work it would still be "7 : 30"

astral lion
#

Oh

#

You're right

#

let me see if it passes

#

it didn't 💀

#

would returning an error help?

#

like

#

if mins >= 60:
print("Error")

lilac lynx
#

that was my first guess

#

but i find this very strange if you do the tests manually your code is passing all tests.

#

probably their test are broken or doing something which they did not told you

lilac lynx
astral lion
#

No clue what that means so I prefer to wait until I learn it on the course

#

Did it with ifs

#

I think I should take input out of the convert function

#

FFS

#

LMAO

#

@lilac lynx

#

Took input from the convert function to main

#

passed all the tests

#

@lilac lynx Thanks for all the help!!

#

Final Code:

#
def main():
    time = input("What time is it? ")
    dtime = convert(time)
    if 7 <= dtime <= 8:
        print("breakfast time")
    elif 12 <= dtime <= 13:
        print("lunch time")
    elif 18 <= dtime <= 19:
        print("dinner time")

def convert(time):
    hour, mins = time.split(":")
    flhour, flmins = float(hour.strip()), float(mins.strip()) / 60
    if flmins >= 1:
        result = 0
    elif flmins < 1:
        result = flhour + flmins
    return result


if __name__ == "__main__":
    main()
lilac lynx
fickle groveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.