161k views
1 vote
#Imagine you're writing some code for an exercise tracker. #The tracker measures heart rate, and should display the #average heart rate from an exercise session. # #However, the tracker doesn't automatically know when the #exercise session began. It assumes the session starts the #first time it sees a heart rate of 100 or more, and ends #the first time it sees one under 100. # #Write a function called average_heart_rate. #average_heart_rate should have one parameter, a list of #integers. These integers represent heart rate measurements #taken 30 seconds apart. average_heart_rate should return #the average of all heart rates between the first 100+ #heart rate and the last one. Return this as an integer #(use floor division when calculating the average). # #You may assume that the list will only cross the 100 beats #per minute threshold once: once it goes above 100 and below #again, it will not go back above.

1 Answer

6 votes

Answer:

Following are the code to this question:

def average_heart_rate(beats):#defining a method average_heart_rate that accepts list beats

total=0 #defining integer variable total,that adds list values

count_list=0#defining a count_list integer variable, that counts list numbers

for i in beats:#defining for loop to add list values

if i>= 100:#defining if block to check value is greater then 100

total += i#add list values

count_list += 1# count list number

return total//count_list #return average_heart_rate value

beats=[72,77,79,95,102,105,112,115,120,121,121,125, 125, 123, 119, 115, 105, 101, 96, 92, 90, 85]#defining a list

print("The average heart rate value:",average_heart_rate(beats)) # call the mnethod by passing value

Output:

The average heart rate value: 114

Step-by-step explanation:

In the given question some data is missing so, program description can be defined as follows:

  • In the given python code, the method "average_heart_rate" defined that accepts list "beats" as the parameter, inside the method two-variable "total and count_list " is defined that holds a value that is 0.
  • In the next line, a for loop is that uses the list and if block is defined that checks list value is greater then 100.
  • Inside the loop, it calculates the addition and its count value and stores its values in total and count_list variable, and returns its average value.
User Jim Clay
by
5.0k points