



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Files to submit: hw2.py Functions to include in hw2.py:
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)
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.
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)
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
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.
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
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
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:
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
Answer these questions commented out in your hw2.py file. Convert 1010010 to decimal. Show your work. Convert 35 to binary. Show your work.
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.
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