PUG – Python User’s Group

Public Group active 5 days, 19 hours ago

PUG – Python User’s Group

Group logo of PUG – Python User’s Group

Speeding Ticket Calculator in Python

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #79169
    Maryam Agalarova
    Participant

    Hi,

    I’m posting here to see if anyone can help me with the following problem:

    “You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 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. Define a function “caught_speeding(speed, is_birthday)” that returns the value of your ticket.”

    I know how to assign speed scales but not sure how to add the birthday function.

    Thank you!

    Maryam

     

     

     

     

    #79210

    One of the possible options could be this – one of the arguments of the function “caught_speeding” is “is_birthday” – so in the input, for example, you can ask whether it is someone’s birthday, and if they reply ‘Yes’, then you add 5 to the speed and calculate the results, otherwise (else), the calculation is done with the original speed – something like this:

    def caught_speeding(speed,is_birthday):
         if is_birthday == 'Yes':​
            speed_updated = speed + 5​​
            if speed_updated < 60:​
               return 'Zero_Updated'​
            if speed_updated > 60:​
               return 'One_Updated'​
         else:​
            if speed < 60:​
               return 'Zero'​
            if speed > 60:​
               return 'One'

    Calling the function with whether the birthday is ‘yes’:
    caught_speeding(50, 'Yes')
    returns ‘Zero_Updated’

    And calling the function with whether the birthday is ‘no’ or any other string:
    caught_speeding(50, 'No')
    returns ‘Zero’

    Hope this is helpful and please let know if anything else could be helpful –

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.