-1
def main(): time= input("What time is it?: ") hour, min= time.split(":") if "a.m." in time: Min, t= min.split(" ") if 7 <= convert(hour, Min) <= 8: print("It's breakfast time.") elif "p.m." in time: if 12 <= convert(hour, Min) < 13 : print("It's lunch time.") elif 6 <= convert(hour, Min) <=7: print("It's dinner time.") else: return else: if 7 <= convert(hour, min) <= 8: print("It's breakfast time.") print(convert(hour,min)) elif 12 <= convert(hour, min) <= 13: print("It's lunch time.") elif 18 <= convert(hour, min) <= 19: print("It's dinner time.") def convert(h, m): H = int(h) M = int(m) Time = float(H+(M/60)) return Time if __name__ == "__main__": main() 

Error message returned from check50:

:( convert successfully returns decimal hours expected "7.5", not "Error\n" 

3 Answers 3

1

The program does not work according to the spec! From the spec:

convert is a function (that can be called by main) that converts time, a str in 24-hour format....

This convert function takes 2 string parameters. It returns the correct answer, but the function signature is not what is defined in the spec (def convert(time):)

1
  • To paraphrase what @DinoCoderSaurus said -- the pset spec says convert() should have ONE input value: 'time', a string in 24 hour format. Your convert() function has 2 input values; convert(h, m) -- or convert(hour, min) Commented Jan 27 at 18:22
1

The code below mimics the check50 testing process. You will get an error message that calls your implementation of convert().

from meal import convert time = '7:30' ans = convert(time) print(ans) 
1

While not 'bugs' per say you have a lot of problems worth addressing in your code

Using variables like both Min and min in the same scope is a bad idea someone reading or editing your code could easily confuse which one is which.

Strongly suggestion you pick a standard for your variable names. either all lowercase or all upper or all words or all nonsense, or snake case or camel case... just pick something standard... I'd strongly suggest snake_case in python. variables should be whole-words with meaning.

be consistent about your spacing.. run your code through style50. consistency is key...

You also call functions too many times for no reason. Suggest you call convert once and save the result. Then perform comparisons on that result as often as you need to. Imagine that everytime you call a function work has to be done, why would you want to redo that work when you don't need to? You will learn about some ways to reduce this later, but in this case you have one input, and only one conversion has to happen. Why call convert multiple times instead of saving the value?

Lastly, 'my program works but fails the test' is not a question and has in my experience almost never been an accurate statement on this site. 99% of the time the user has done something very simple very wrong, this is no exception. The program doing what you think it should is not the question, the program doing what the spec says it should is the question. And as mentioned by others you have the wrong signature for your function. Period. You didn't follow instructions, and you did not solve the problem, hence the tests fail.

Passing the tests is every bit if not more important than getting the code to do what you think it should. If your code is not testable and not readable than you haven't learned the key skills the exercise is here to teach you and test you on. Remember even a broken clock is right twice a day. Your goal is to learn the language passing the assignment is a step towards that; your goal was not to make a program that converts time however you felt like converting it.

You need to do exactly what the spec asks you to do, no more, no less. You need to pass unit tests, you should generally want to write your own tests once they cover that, potentially in addition to ones they provide you with. Demonstrate that your code can't not work through testing whenever you can do so reasonably, and you will be golden. It is rather trivial here to set up a test which would attempt every possible input value and validate if it was correct or not. Why not do that? It's simple and fast for the computer to run.

Always double check: did I do what I was asked to do not what you think you were asked, what you were asked.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.