ajay sharma
ajay sharma

Reputation: 19

how to use loop in list inside list

3 participant makes 3 round of ground after every round heartbeat of every participant has been recorded. have to find the average of heart beat of every participant after 3 rounds. How to write this program.

Upvotes: 1

Views: 53

Answers (3)

Muhammad Irfan Shahzad
Muhammad Irfan Shahzad

Reputation: 115

# p1 for participent1 p2 for participent2 p3 for participent3 
HeartBeatP1=70 #let initial heart beat
HeartBeatP2=65
HeartBeatP3=75
P1=[] #list for store heartbeat participent  1
P2=[] #list for store heartbeat participent 2
P3=[] #list for store heartbeat participrnt 3
for i in range(3):
    P1.append(HeartBeatP1)
    HeartBeatP1+=10 #After every round heart beat increase 10 BPM
    P2.append(HeartBeatP2)
    HeartBeatP2+=10
    P3.append(HeartBeatP3)
    HeartBeatP3+=10
#calculate average by accessing value participent list
AverageP1=(P1[0]+P1[1]+P1[2])/3 
AverageP2=(P2[0]+P2[1]+P2[2])/3
AverageP3=(P3[0]+P3[1]+P3[2])/3
print("Average for participent 1 :",AverageP1)
print("Average for participent 2 :",AverageP2)
print("Average for participent 2 :",AverageP3)

Upvotes: 1

Programmer
Programmer

Reputation: 71

It is not a difficult program to write but anyways:

heart_beat = [(70, 80, 90), (75, 76, 77),(90, 91, 92)] # Every tuple in the list represent the heart beat recordings for three different rounds.


def compute_avg(beat):
    return sum(beat)/len(beat)

pl_1, pl_2, pl_3 = [], [], []
for p1, p2, p3 in heart_beat:
  pl_1.append(p1)
  pl_2.append(p2)
  pl_3.append(p3)


avg_pl_1, avg_pl_2, avg_pl_3 = compute_avg(pl_1), compute_avg(pl_2), compute_avg(pl_3)

Upvotes: 0

Sushil
Sushil

Reputation: 5531

If you are interactively getting the heartbeat values from the user, you can try something like this:

heartbeat_lst = []

for x in range(3):
    heartbeat = input(f"Enter the heartbeats of participant {x+1} separated by a comma (in BPM)").split(',')
    heartbeat = [float(value) for value in heartbeat]
    heartbeat_lst.append(heartbeat)

[print(f"Mean of participant {participant_num+1} = {round(sum(heartbeatt)/len(heartbeatt),2)} BPM") for participant_num,heartbeatt in enumerate(heartbeat_lst)]

Output:

Enter the heartbeats of participant 1 separated by a comma (in BPM)>? 75,89,100
Enter the heartbeats of participant 2 separated by a comma (in BPM)>? 98,79,110
Enter the heartbeats of participant 3 separated by a comma (in BPM)>? 100.2,112.6,109
Mean of participant 1 = 88.0 BPM
Mean of participant 2 = 95.67 BPM
Mean of participant 3 = 107.27 BPM

Upvotes: 0

Related Questions