I have a homework project where I need to find the max, min, mean and median of a list. I'm not allowed to use ready functions, so I tought using "if, else, elif" statements. In my code, I'm getting a syntax error on the mean operation "elif i += mean_trip" with "+=", and I'm having trouble to find out how to get this line right. The others operations are outputted correctly
I saw a version of the project where the person uses the statements, but separate for each operation. I decided to condense everything into one.
trip_duration_list = column_to_list(data_list, 2)
min_trip = 0.
max_trip = 0.
mean_trip = 0.
median_trip = 0.
new_list = sorted(float(i) for i in trip_duration_list)
new_list_size = len(new_list)
index = (new_list_size -1) // 2
for i in new_list:
i = float(i)
if min_trip == 0:
min_trip = i
elif i < min_trip:
min_trip = i
elif max_trip == 0:
max_trip = i
elif i > max_trip:
max_trip = i
elif i += mean_trip:
mean_trip = mean_trip / len(new_list)
elif new_list_size % 2:
median_trip = new_list[index]
else:
median_trip = (new_list[index] + new_list[index+1]) / 2
Aucun commentaire:
Enregistrer un commentaire