Activity 2-2

For this activity, we will write a program that computes how many calories a person burns when they run. This program will ask the user for their weight and how fast they run a mile. It will convert these values to the correct units and print out the answer.

Task 1: Create a new Python script

Create a new python script and name it FIRSTNAME_LASTNAME_act2-2.py. Remember to place the following line at the top of your script. It's not completely necessary, but it's helpful for your computer to understand how to run the program.

#!/usr/bin/env python3

Task 2: Store some values to constants

Assigning constant numbers to variables allows us to give names to specific values, so while writing a program, it is easier to understand the formulas we use. For example, we will create a variable to keep track of the number of meters per mile. This is a much better practice than placing the value 1609 directly in the formula we place it in our program because it will lack the context provided by the variable name.

meters_per_mile = 1609

When creating variable names, Python has strict rules for naming variables, and our course has a few more. Python won't run your code if you don't follow its rules and won't care about our rules, which are just good guidelines for a consistent style. We will also deduct points if you don't follow our rules.

Python's Rules for creating variable names

  • Variable names must contain only the characters a-z, A-Z, 0-9 and underscores
  • A letter must be the first character (98_degrees is not valid, but boyz_2_men would be)
  • Variable names are case sensitive

CS0030's style guide rules

  • Only use lowercase characters
  • Separate words with underscores (_)
  • Variable names should contain full words or common abbreviations (meters_per_mile, not m_per_mi)
  • Don't use a reserved Python word (anything that is colored in your text editor)
  • Variable names should include a word that eludes to its type:
    • integer: e.g. count, number, index, etc.
    • float: e.g. quantity, amount, size, weight, mass, etc
    • string: name, title, word, sentence, description, etc
    • boolean: is_ascending, is_sorted, is_full, etc

Assign the following values to variables with meaningful but concise names. each variable assignment should occur on their own line.

  • 1609: the number of meters in a mile
  • 4.86: the respiratory exchange ratio
  • 2.2: the number of pounds in a kilogram

Read in input

This program requires two pieces of information from the user running the program

  • The number of minutes it takes for them to run a mile
  • Their weight in pounds

Use input() to ask the user for each piece of information, and assign them to their own variables. input() can accept an optional string input, which it will print before accepting input from the user. Then, after being prompted with the input question, the user running the program will then be able to type whatever they want, and when they press enter input() will return what ever the user typed. Importantly, whenever you use input() it reads in each value as a string even if you type only a number. To use it in our calculations, you must convert it to a float. You can do that with the float() function. Again, your variable names should be in all lowercase with words separated by underscores.

For example, to collect and assign the user's mile time to a variable, we could write

minutes_per_mile_string = input("How many minutes does it take for you to run a mile? ")
minutes_per_mile = float(minutes_per_mile_string)

Perform the same task to collect the user's weight

To keep code readable, it is often helpful to manually wrap lines of code. In the above lines of code, the first line is actually 88 characters long. As lines of code get longer and longer, it makes it harder to read. We have difficulty tracking lines of code horizontally. Therefore, it is often a requirement in style guides used by professional developers to keep lines of code to a strict limit. For this course, we ask that you keep your lines of code to 80 characters.

Python will attempt to interpret each line of code as its own expression, so when you break a line of code, you must tell Python explicitly that you are including a line break. You can do this by using a backspace character (\). For the next line of code, it should be indented to help our eye see that it is a continuation of the previous line. Sublime and Atom should automatically indent, but if not, press tab to indent the line.

minutes_per_mile_string = \
    input("How many minutes does it take for you to run a mile? ")

minutes_per_mile = float(minutes_per_mile_string)

Task 3: Test out the program

Use print() to print out the two number variables you just created in task 2. For example, you can print out the minutes per mile value with print(minutes_per_mile). Run your program at this point to ensure that it works as expected and doesn't have any errors. It should ask the user for the two pieces of information and then print it out.

Once you are confident the program works as expected, remove the print statements and continue on

Task 4: Convert the different values to other values

We need to convert the user's mile time to a running speed with units of meters per minute. We can do this in two steps. First, we convert mile time to the time it takes to run a meter. Divide the minutes per mile (minutes_per_mile) variable by the meters per mile (meters_per_mile) to calculate the user's time to run 1 meter. For example, I would write the following

minutes_per_meter = minutes_per_mile / meters_per_mile

Be sure to replace the variable names above with the variable names with those that you defined yourself.

Then, to calculate their speed in meters per minute, you just need to invert the minutes per meter value.

meters_per_minute = 1.0 / minutes_per_meter

Convert their weight in pounds to their weight in kilograms. You will need to divide their weight in pounds by 2.2, the number of pounds in a kilogram. You stored this value to a variable in a previous task. Be sure to use your variable instead of the actual number.

Task 5: Test out the program again

Before continuing on, add print statements to print out the user's speed in meters per minute and their weight in kilograms variable. Run the program again, this time after asking for the user's inputs, it should print out those two values. If your program throws any exceptions, or it doesn't output the correct values carefully look at your code to identify any issues. Try using 155 pounds for the weight and 10 minutes for the mile, it should come out to 70.5 kilograms and 160.9 meters per minute.

Task 6: Perform the final calculation

Estimate their oxygen consumption, with the following formula

Estimated O2 consumption = 0.2 * (running speed in meters/min) + 3.5

Now you can estimate their final calories per minute burned when running. It is just 0.005 * (weight in kg) * (O2 consumption). Assign their calories burned per minute value to a variable.

Output the converted values using string concatenation

String concatenation in Python requires that each value that is concatenated must be of type string. Therefore, to print the sentence, "You burn approximately 15 calories per minute", you would have to write something like:

print("You burn approximately " + str(calories_per_minute) + " calories per minute running")

In a nicely worded sentence like the example above, print out their weight, their running speed, and the amount of calories burned per minute

Task 7: Test your program

We need to further verify that the program works correctly (even if it can run completely through without an exception). Put in a weight of 155 lbs and 10 minutes for the weight and mile time. It should result in about 12.5 calories/minute. Then try 180 lbs and 8 minutes. The result should be about 17.9 calories/min


Once you're done, either show your working Python file to a TA in class or upload it to your google drive and share it with cs0030handin@gmail.com by midnight, 2/23 to be checked off for this activity.