An interactive introduction to programming in Python, for human beings and whoever else
Project
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:
Reckless drivingReckless driving (e.g. 50 in a 30 zone)SpeedingToo slowJust rightNote: 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)
input to ask how fast the user is goingspeed = input("How fast were you going?") will save the user’s input to a variable called speedinput 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.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)elif when checking the speed.if or elif that works, it doesn’t run any of the other options. The order that you check in will probably be important!