I'm trying to have this while loop give the same output as the for-loop, however, its infinitely recursive as opposed to stopping at the maximum average height.
The code looks like this:
import numpy as np
N = 5
h = np.zeros(N) # heights of family members (in meter)
h[0] = 1.60; h[1] = 1.85; h[2] = 1.75; h[3] = 1.80; h[4] = 0.50
sum = 0
i = 0
while i < 5:
sum = sum + h[i]
average = sum/n
print('average height: {:g} meter'.format(average))
This is the for loop I was trying to get the while loop of:
for i in [0, 1, 2, 3, 4]:
sum = sum + h[i]
average = sum/N
print(’Average height: {:g} meter’.format(average))
Where did I go wrong?
expected output:
average height 0.64 meter
average height 1.01 meter
average height 1.36 meter
average height 1.72 meter
average height 1.82 meter