Speeders

Speeding Tickets

Conditional Statements


Situation


There has been reports of cars speeding in the town of PyVille. A cop records the speed of 10 cars that pass by him.


speeds = [45, 55, 35, 52, 43, 47, 59, 93, 22, 76]



Task 1: Print out each speed in the list


The police chief needs a report of the speeds. Write a program that prints out each speed in the speeds list.

speeds = [45, 55, 35, 52, 43, 47, 59, 93, 22, 76]

for speed in speeds:
    print(speed)

Task 2: Give out Tickets


Now you need to give out tickets to those who are going over the speed limit of 45mph. Write a program that prints out 'You got a ticket' if the speed is over 45mph.

speeds = [45, 55, 35, 52, 43, 47, 59, 93, 22, 76]

for speed in speeds:
    if speed > 45:
        print('You got a ticket')

Task 3: Give Feedback


Next depending on the speed of the car give feedback to the driver.

If the driver is going under the speed limit, print 'Go Faster'.

If the driver is going over the speed limit, print 'Slow Down'.

If the driver is going the correct speed, print 'Great Job!'

speeds = [45, 55, 35, 52, 43, 47, 59, 93, 22, 76]

for speed in speeds:
    if speed < 45:
        print('Go Faster')
    elif speed > 45:
        print('Slow Down')
    else:
        print('Great Job!')

Task 4: Chase the bad guy


Once you are caught speeding the cop has a 60% chance of catching you. Write a program that prints “You got caught”, “You outran the cops”, or “The cops left you alone” depending the the situation.

from random import randint

speeds = [45, 55, 35, 52, 43, 47, 59, 93, 22, 76]

for speed in speeds:
    if speed <= 45:
        print('Speed:', speed, 'The cops left you alone')
    else:
        catch = randint(1,100)
        if catch < 60:
            print('Speed:', speed,'You got caught')
        else:
            print('Speed:', speed,'You got away')