QuestionQuestion

1. Activity tracker
The questions in this homework assignment all relate to the same imaginary scenario in which you work for FitBit, a company that makes activity trackers that count the number of steps you take each day and provide feedback about your progress. Below, we'll explore how such feedback can be created!

1a. Creating a function
At FitBit, you're working on motivational messages to provide to users. Specifically, you'd like to create a function that (1) computes the average number of steps taken across a one-week period, and (2) prints a message to the user about that average.
In the cell below, create the function week() that takes a single input, steps . Note that steps will be a list of steps taken each day. Remember, your function should include variable names only, and no actual values.

Your function should include a docstring indicating that the function's purpose is to print the average number of steps per week. Then do the following:

Compute the average number of steps taken that week
Hint: Review the lecture on lists, including how to sum up the contents of a list, and how to determine the length of a list. The sum divided by the length will give you the average.
Round the resulting value up
Hint: You'll need to import the math module
Convert this rounded value to a string when printing the following message: This week, you took an average of x steps!
Instead of x , use the variable representing the rounded average you computed above. Remember that if you want to add a number and a string together, you first need to convert the number to a string.

In [ ]:

In the cell below, I've created the variable steps for you. Use this variable as input to test your function.

In [ ]:

1b. Multiple inputs + conditional execution
In addition to providing weekly messages about step counts, you also want to send the user a message at the end of each day. Importantly, the specific message will depend on whether they've taken more steps today than yesterday.
In the cell below, create the function day() that takes two inputs: tod and yest , representing the number of steps taken today and the number of steps taken yesterday. Just like last time, your function should include variable names only, and no actual values.

Your function should include a docstring that describes the function's purpose, and then code that does the following:

If the number of steps taken today is greater than yesterday, print the following:
Great! You walked x steps. Better than y steps from yesterday!
Otherwise, print the following:
You walked x steps. Not quite as good as y steps from yesterday.
Note: Rather than print x or y in the above messages, you should instead print the relevant number of steps.

In [ ]:

In the cell below, run your function twice: The first time, use your choice of inputs to produce the "Great!..." message, and the second time, use two inputs that will produce the "You walked..." message.

In [ ]:

2. Getting fancy

2a. Using return()

In the cell below, copy your code from question 1a and make the following changes:

Rename the function to be week_return()
Delete the portion of code in which you print a message to the user Write a new last line in which you simply return the rounded value
Note: You're not going to test the function directly. Instead, you'll use this function in question 1d as part of another function.

In [ ]:

2b. Function within a function
See Lab8 if you need a review on using a function within a function.

In the cell below, create the function motivate() that takes two inputs: this_wk and last_wk , which represent a list of steps taken each day this week and a list of steps taken each day last week, respectively. The point of this function will be to compare the average number of steps taken this week vs. last week, and provide a motivational message. As always, remember to only use variable names in your function, and not actual values.

In your new function, start by running the function you just created with this_wk as input. Remember, whenever you run a function and you expect output to be returned, you need to assign that output to a variable name of your choosing. For now, I'll refer to this as your "first output".
Next, run week_return() , but this time with last_wk as input. Again, assign the output to a variable of your choosing. I'll refer to this as your "second output".
Now you're ready to write conditional code that provides one of two messages. If the value assigned to your first output is greater than your second output, print the following:
Great! You walked x steps this week. Better than y steps from last week.
Otherwise, print the following:
You walked x steps this week. Not quite as good as y steps from last week.
Note: Rather than print x or y , you should print your first and second outputs, respectively.

In [ ]:

Now, you're ready to run your function using the following inputs:

In [ ]:

3. Step stats
For this final question, imagine that Fitbit has asked you to write a function that returns three values: the lowest number of steps taken this week, the highest number, and the average number.

Create a function named step_stats() that takes a single input: this_wk , representing the number of steps taken each day this week.
Write code that will calculate the lowest, highest, and average number of steps taken this week. Return all three values (no need to write any messages to the user)

In [ ]:

Below, I've copied and pasted the same list as above for this_wk . Use this as input for running your function. After running the function, print a message that says:

Your steps ranged from x to y, with an average of z
Rather than x , y , and z , you should include the values returned from your function.

In [ ]:

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

: import math
'''
Method week(steps):
Input steps is a list of steps taken each day
Calculates and prints the average of steps per day
'''
def week(steps):
average = math.ceil(sum(steps) * 1.0 / len(steps))
print("This week, you took an average of " +
str(average) + " steps!")

# Run function
steps = [4039, 5018, 4596, 6001, 5722, 5371, 4839]
week(steps)

'''
Method day(tod, yest):
Input tod is an integer of number of steps taken today
Input yest is an integer of number of steps taken yesterday
Prints a message comparing steps today & yesterday
'''
def day(tod, yest):
if tod > yest:
print("Great! You walked " + str(tod) +
" steps. Better than " + str(yest) +
" steps from yesterday!")
else:...

By purchasing this solution you'll be able to access the following files:
Solution.pdf.

$75.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats