mazda protege
Several exercises in this module relate to cars! This is an image of your instructor's favorite car marooned in Southern Utah. See more photos of Nedfry here.
techred home > CSC-101-DA

Python Road Trip: Variables and conditional logic in python

Explore python fundamentals with exercises that involve travel calculations such as gas mileage, travel costs, and more.

Learning Objectives

check_box

Create variables in python and initialize them with both hard-coded values and user-input.

check_box

Use essential mathematical opeators to manipulate variables of varying types.

arrow_upward


Part 1: Hard-coding variable and using basic operators

  1. Create a new python file called roadtrip.py using either in your IDE or using repl.it
  2. Create a comment block at the top of your file (using # characters) that contains your name, the course name, and the purpose of this code: to demonstrate variable assignment operations and basic operators
  3. Write a python expression that displays the text "Road trip efficiency comparisons"
  4. Declare a variable named small_vehicle and assign to that variable a string describing the year, make, and model of a small car you find interesting. This can be a hybrid or an EV or a regular old ICE car. For example you could assign the string "1996 Mazda Protege" to small_vehicle
  5. Choose a large_vehicle year, make, and model and declare that variable and assign to it a string describing your vehicle of choice.
  6. Navigate to the EPA's official mileage efficiency listings for vehicles on American roads. Look up the year, make, and model of your small car and large car variables.
  7. Declare two variables called small_vehicle_combined_mpg and large_vehicle_combined_mpg and assign to them the EPA's combined mileage rate
  8. Create two more variables of appropriate names that contain the URL to a website featuring a photo of your chosen vehicles. For example, if you chose the Ford Maverick pickup, you could have a variable called large_car_url and assign to it "https://www.ford.com/trucks/maverick/"
  9. Now write a set of print() expressions that display to the console the basic facts about each of your chosen vehicles. The output should look something like the clip below. Remember, don't retype the vehicle information or mileage rates. Rather, access the variables you made in your print() statements.
screen clip of output

arrow_upward


Part 2: Compute road trip metrics

  1. Divide the output from part 1 from this output by printing out a row of some dashes like ----------------
  2. Prompt the user of your program with a printed statement to enter the total miles of a proposed road trip.
  3. Then read into a variable of a sensible name the number of miles of the trip. Your variable could be called trip_miles. Immediately cast the user's input to a float (a number with a decimal point) with code like this trip_miles = float(input())
  4. Compute the total number of gallons of gasoline required to undertake a trip of the inputted number of miles in both your small and large vehicles. Do this by writing two python expressions, each of which uses the = assignment operator to store the result of dividing trip length by miles per gallon in the corresponding variable on the left. Your new variables can be called something like small_vehicle_gallson and large_vehicle_miles. For example: small_vehicle_gallons = trip_miles / small_vehicle_combined_mpg
  5. Display the results of your two gallon requirements in two additional lines of output, one for each car, that outputs the vehicle type, the trip length, and the gallons of gas required.
  6. Compute how many more gallons the large car will require than the small car. Use the subtraction operator and store the result of your subtraction in a sensibly named variable. Display the result after your gallon requirements are displayed.
  7. Use the sample output below to help guide your coding.
screen clip of output

Part 3: Fuel costs and expected travel times

  1. Print another line of dashes to separate out part 2 from this part 3
  2. Ask the user for the average cost of a gallon of gasoline expected on the trip.
  3. Now use the float(input()) pattern as in part 2 to read in the price per gallon of gasoline into a sensibly named variable.
  4. Compute the total trip cost for each vehicle using the multiplication operator and the basic expression that total cost of trip = gallons of fuel required x price per gallon of fuel. Use sensibly named variables to store your result.
  5. Just like in part 2, create three lines of output: one line displaying the total expected trip cost for each vehicle and the third line comparing the difference in trip costs.
  6. Create two hard-coded variables that store the expected driving speed of cars on two types of roads: US interstate highways (the big, access-controlled roads) and local roads.
  7. Optional extension: Ask the user for their expected speeding adjustment rate for each type of road. For example, some folks basically always drive 11 miles per hour over the posted speed limit on interstates and 5 over on local roads. Read these values into their own variables. Then compute two "actual speed" variables that add the hard-coded speeds per road type to the user's speeding buffers.
  8. Compute expected travel time for the trip of the given length on both types of road, taking into account the expected speeding. Travel time can be calculated by dividing total trip miles by travel rates in miles per hour.
  9. Display the results of your travel time computations for each type of car on each type of road.

Link to code

For reference, here's some help code for the above exercises.

Extension exercises

For students who have python experience or new programmers with lots of gusto, implement one or more of these features in our road trip program.

Add conditional logic for warnings

Implement with dictionaries

Carbon footprints of various modes of travel

Find a tool that compares the carbon output of various modes of travel such as this one by sustainable travel international. The modify your program to not only display how many gallons of gas are required to undertake a trip but also displays the total weight of carbon that trip with that car will input into the atmosphere. You could also try asking the user for a total trip distance and then provide a comparison of the carbon output to undertake that trip in a vehicle versus an airliner versus a boat of some kind.