Python's Not (Just) For Unicorns

An interactive introduction to programming in Python, for human beings and whoever else

Project

Project - Speeding robot

In Virginia, there are two punishments for speeding in a car: a speeding ticket (bad), or reckless driving (worse!). We’re going to build a robot to warn people about their driving habits.

Ask the user how fast they are going, print out their speed, then print out their status according to these guidelines:

  • Going over 80mph? Reckless driving
  • Going 20mph or more over the speed limit? Reckless driving (e.g. 50 in a 30 zone)
  • Going over the speed limit? Speeding
  • Going more than 5mph under the speed limit? Too slow
  • Everything else? Just right

Note: You cannot be charged with both reckless driving and speeding. If you’re going fast enough to be reckless, it’s just reckless driving.

The code I’ve given you randomly picks a speed limit1 and prints it to the screen. After that, it’s all up to you! Start by asking the user how fast they’re going.

[1] Technically the code imports some other code that lets you use random numbers, then picks a number between 15 and 65, going in steps of 5 (15, 20, 25, 30…)

import random
random.seed(6779)
speed_limit = random.randrange(15, 66, 5)
print("The speed limit is", speed_limit)
  • Hint: Use input to ask how fast the user is going
  • Hint: speed = input("How fast were you going?") will save the user’s input to a variable called speed
  • Hint: input always gives you a string, so you’ll need to use int() to convert the user’s input to an integer. If we don’t, you can’t compare it to other numbers! That’ll probably give you an unorderable types error about str and int.
  • Hint: You can convert speed to an integer every time you compare - like int(speed) > speed_limit - or you can convert just once and overwrite the original value, like speed = int(speed)
  • Hint: You’ll probably want to use elif when checking the speed.
  • Hint: Once Python finds an if or elif that works, it doesn’t run any of the other options. The order that you check in will probably be important!