CS 1301 Spring 2008 Homework 2: Running Functions and Calculating MPH and Calories Burned , Assignments of Computer Science

The instructions for homework 2 in cs 1301 spring 2008 course. Students are required to write python functions to calculate their miles per hour (mph) and calories burned based on the number of miles they ran and the time it took them to run. The assignment also includes functions to print and return stars, add numbers, and print a name. Students are encouraged to test their functions and use conditionals to print messages based on their mph.

Typology: Assignments

Pre 2010

Uploaded on 08/04/2009

koofers-user-p5q-1
koofers-user-p5q-1 🇺🇸

9 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Homework 2 CS 1301 – Spring 2008 Page 1/5
CS 1301 – Spring 2008
Homework 2 – Running Functions, the Python Marathon
Due: Thursday, January 31, 2008 at 6 PM EST
Files to submit: hw2.py
Functions to include in hw2.py:
1. mph()
2. caloriesBurned()
3. main()
4. printStars()
5. returnStars()
6. printAdd()
7. returnAdd()
8. useAdd()
9. printName()
For Help:
T-Square Forum – see course syllabus for more detailed information
TA Helpdesk – schedule is posted on class website
Email TAs
Notes:
Think through the logic of each of the problems before you start coding anything
Don’t forget to include the required comments and collaboration statement (as outlined
on the course syllabus)
Do not wait until the last minute to do this assignment in case you run into problems
If you find an error in the homework assignment, let a TA know immediately
Information you might need:
Remember all of the functions we used for HW1: raw_input(), float(), str()
It might help to look at the comparison operators: http://docs.python.org/lib/comparisons.html
Assume that the user will enter valid data (i.e. no negative or obscenely large numbers)
Background Story:
Your professor decides that for homework, everyone must go run a Pi Mile
(http://dev.crc.gatech.edu/facilities/pimile.pdf) and time how long it takes to run the 3.14 miles.
As you come in from your run and start to stretch, you decide to write a Python program to tell
you just how fast you run and how many calories you’ve burned.
pf3
pf4
pf5

Partial preview of the text

Download CS 1301 Spring 2008 Homework 2: Running Functions and Calculating MPH and Calories Burned and more Assignments Computer Science in PDF only on Docsity!

CS 1301 – Spring 2008

Homework 2 – Running Functions, the Python Marathon

Due: Thursday, January 31, 2008 at 6 PM EST

Files to submit: hw2.py Functions to include in hw2.py:

  1. mph()
  2. caloriesBurned()
  3. main()
  4. printStars()
  5. returnStars()
  6. printAdd()
  7. returnAdd()
  8. useAdd()
  9. printName() For Help:  T-Square Forum – see course syllabus for more detailed information  TA Helpdesk – schedule is posted on class website  Email TAs Notes:  Think through the logic of each of the problems before you start coding anything  Don’t forget to include the required comments and collaboration statement (as outlined on the course syllabus)  Do not wait until the last minute to do this assignment in case you run into problems  If you find an error in the homework assignment, let a TA know immediately

Information you might need:

Remember all of the functions we used for HW1: raw_input(), float(), str() It might help to look at the comparison operators: http://docs.python.org/lib/comparisons.html Assume that the user will enter valid data (i.e. no negative or obscenely large numbers)

Background Story:

Your professor decides that for homework, everyone must go run a Pi Mile (http://dev.crc.gatech.edu/facilities/pimile.pdf) and time how long it takes to run the 3.14 miles. As you come in from your run and start to stretch, you decide to write a Python program to tell you just how fast you run and how many calories you’ve burned.

The point of this assignment is to get you thinking about the differences between printing and returning.

Part 1 - mph

Write a function, mph that takes in two parameters, milesran and time. Recall the syntax for setting up a function: def mph(milesran, time): This function should return a value for miles per hour, calculated using the parameters for the number of miles and time in minutes. You can test your function in the shell like so:

mph(3.14, 23)

mph(5.4, 55)

Part 2 - caloriesBurned

Now we will write a function to calculate and return how many calories the runner burned based on the number of miles ran. For every mile that you run, you burn 100 calories (no matter how fast you run). Use this fact to calculate how many calories you burn.

caloriesBurned(3.14)

caloriesBurned(5.4)

caloriesBurned(8) 800

Part 3 - main

Prompt the user for the number of miles he or she runs and the time it takes to run those miles in minutes using raw_input. Use these values in your functions mph and caloriesBurned to calculate the runner’s speed and calories burned. If you do not use your functions you will get points taken off. Print out the runner’s miles per hour to two decimal places and calories burned to one decimal place using the %f text formatting as seen in lecture.

returnStars(3) '***' returnStars(10) '**********' What is the difference between the output of printStars and returnStars? Answer this question, commented out in your hw2.py file.

Part 6 – printAdd

Write a function called printAdd that takes in two parameters and then prints the summation of the two parameters. (You may use + in this function.) Sample output:

printAdd(2,3) 5

Part 7 – returnAdd

Write a function called returnAdd that takes in two parameters and then returns the summation of the two parameters. (You may use + in this function.) Sample output:

returnAdd(2,3) 5

Part 8 – useAdd

Now we will use both printAdd and returnAdd in a trivial function to see what happens. Get pumped. Write a function called useAdd that takes in two parameters. Print out the function printAdd using the two parameters from useAdd. Then print out the function returnAdd. Next just call printAdd (don’t actually “print” it). Finally call returnAdd. Observe the differences. Sample output:

useAdd(3,2) This is printAdd's output printed: 5 None This is returnAdd's output printed: 5 This is printAdd's output without print: 5 This is returnAdd's output without print:

Part 9 – printName

Write a function called print name that takes in one parameter, a name. Print out a line of stars equal to the number of characters in the inputted name. (You may find len() to be helpful). On a new line print the name. Then on the next line, print out another line of stars the same length as the name. Sample output:

printName("Monica")


Monica


Part 10 – Binary Questions

Answer these questions commented out in your hw2.py file. Convert 1010010 to decimal. Show your work. Convert 35 to binary. Show your work.

Bonus – Level: Easy

Write an additional function, avgmiles(milesran, time) that calculates your average mile speed for example if you run 3 miles in 30 minutes, the function would return that you run 10 minute miles). Use this function in main() and print out the average mile speed with the rest of the output.

Final thoughts

Be sure to name your files and functions exactly as indicated. READ THROUGH THE INSTRUCTIONS CAREFULLY. If you need help, be sure to come to office hours. Good Luck! Written by Beth York, Spring 2008