Docsity

Introduction to Computing - Running Scripts and Functions | CS 1301, Assignments of Computer Science

Toggle sidebar
bg1
Homework 2 CS 1301 – Fall 2007 Page 1/4
CS 1301 – Fall 2007
Homework 2 – Running Scripts and Functions
Due: Friday, September 7, 2007 at 6 PM EST
Files to submit: hw2.py
mph.py
Functions to include in hw2.py:
1. mph(milesran, time)
2. caloriesBurned(milesran)
3. main()
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 scripts
and functions and between printing and returning.
pf3
pf4

Partial preview of the text

Download Introduction to Computing - Running Scripts and Functions | CS 1301 and more Assignments Computer Science in PDF only on Docsity!

CS 1301 – Fall 2007

Homework 2 – Running Scripts and Functions

Due: Friday, September 7, 2007 at 6 PM EST

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

**1. mph(milesran, time)

  1. caloriesBurned(milesran)
  2. main()** 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 scripts and functions and between printing and returning.

Part 1 – The Script Version

We will start by writing a script to tell the user how many miles per hour he or she runs. DO NOT USE FUNCTIONS IN THIS PART. Prompt the user for how many miles he ran and how long it took to complete the run (in minutes). Do the appropriate calculations with these values and print out his speed in miles per hour. Example:

How many miles did you run? 3. How long was your run? (in minutes) 23 You run at 8.19 miles per hour! Save this script in a file called mph.py

Part 2 – The Functions Version

Now we will write a more complicated version with functions.

1. mph(milesran, time)

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)

2. caloriesBurned(milesran)

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)

And you burned 580.0 calories. What a speed demon you are! Put all of these functions in a file called hw2.py

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, Fall 2007