The question, my answer, and the output is screenshotted here
Constraints:
0=no ticket
1=small ticket
2=big ticket.
If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.
My Code:
def caught_speeding(speed, is_birthday):
if speed > 80 or (speed > 85 and is_birthday):
return 2
return int((60 < speed <= 80) or (65 < speed <= 85 and is_birthday))
caught_speeding(65, True) gives 1 but should be 0
caught_speeding(85, True) gives 2 but should be 1
the other tests are ok. When I trace the code I cant seem to find the error
For
caught_speeding(65, True), the first condition is false so it falls through to thereturnstatement. In that case,(60 < speed <= 80)is true, since 65 falls within that range. The other condition doesn't matter since it's joined with anor, so it's effectively doingreturn int(True)which is1.For
caught_speeding(85, True), thespeed > 80condition is true, since 85 is greater than 80. Again, the other condition doesn't matter since it's joined with anor, so it executes thereturn 2statement.