r/programminghelp • • May 02 '20

Python I need help with this If statement

I am a complete beginner in python and I am doing some code practicing from the website codingbat.com Anyways, I have reached this question and I need help with it

We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0 to 23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return True if we are in trouble.

def parrot_trouble(talking, hour):

Now my question is how to ensure that the number does not exceed above 23 and below 0 and be able to write it in a code

Even the slightest assitance helps. Thanks!

4 Upvotes

4 comments sorted by

2

u/marko312 May 02 '20

This is likely a given, so the input given to that function will never be outside that range.

If you want to be extra sure, you could add something like:

if hour not in range(0, 23 +1):
    raise ValueError(f'Invalid hour: {hour}')

at the beginning of that function.

1

u/Yaman_M May 02 '20

Oh that was easy! Why didn't I think of it lol

3

u/MarkRems May 02 '20

I'm assuming you don't actually personally have to worry about making sure the hour does not exceed 23 and is below 0. What they are telling you is when using the function the caller will only pass in numbers from 0 to 23. All you really have to do is return True if the hour is less than 7 or greater than 20. False otherwise.

1

u/Poddster May 04 '20

Why would you want to check/validate that? Even if the given number is -5, that's still before 7:00, and 26 is still after 20:00!