In: Computer Science
Write a method speedingTicket() that decides whether a driver should be given a ticket from a police officer. The method accepts three parameters: speed (i.e., car speed) as integer; limit (i.e., speed limit) as integer and a boolean named donut indicating whether or not the police officer has eaten a donut today. A police officer that has eaten a donut is happy, so he/she is less likely to give the driver a ticket.
Your method should display either “Give Ticket” or “No Ticket!” based on the following:
• TICKET: Driver’s speed is more than 20 over the speed limit, regardless of donut status.
• TICKET: Driver's speed is 5-20 over the speed limit and officer has not eaten a donut.
• NO TICKET: Driver's speed is 5-20 over the speed limit and officer has eaten a donut.
• NO TICKET: Driver’s speed is < 5 over the speed limit, regardless of donut status.
Sample Calls Output speedingTicket(58, 55, false) No Ticket!
speedingTicket(45, 30, false) Give Ticket
speedingTicket(100, 75, true) Give Ticket
speedingTicket(42, 35, true) No Ticket!
speedingTicket(30, 35, false) No Ticket! //write your method below including the method header
Python 3 code
============================================================================================
def speedingTicket(speed,limit,donut):
diff=speed-limit
if(diff>20):
status='Give Ticket'
elif(diff>=5 and donut == False):
status='Give Ticket'
elif(diff>=5 and donut == True):
status='No Ticket!'
elif(diff<5):
status='No Ticket!'
return status
print(speedingTicket(45, 30, False))
print(speedingTicket(100, 75, True))
print(speedingTicket(42, 35, True))
print(speedingTicket(30, 35, False))
============================================================================================
Output