students = [
{'name': 'Mike', 'age': 20, 'height': 182, 'weight': 77},
{'name': 'Tom', 'age': 21, 'height': 177, 'weight': 72},
{'name': 'Alice', 'age': 19, 'height': 167, 'weight': 52},
{'name': 'Left', 'age': 23, 'height': 184, 'weight': 82},
]
for i in range(len(students)-1):
name = students[i]['name']
age = students[i]['age']
height = students[i]['height']
weight = students[i]['weight']
next_name = students[i+1]['name']
next_age = students[i+1]['age']
next_height = students[i+1]['height']
next_weight = students[i+1]['weight']
difference_height = height - next_height
difference_weight = weight - next_weight
print("{} and {} are {}, {} and the difference in height and weight are {}cm an
{}kg".format(name, next_name, age, next_age, difference_height, difference_weight))
This is the result:
Mike and Tom are 20, 21 and the difference in height and weight are 5cm and -5kg
Tom and Alice are 21, 19 and the difference in height and weight are 10cm and -20kg
Alice and Left are 19, 23 and the difference in height and weight are -17cm and 30kg
How can I convert -17
and -30
to 17
and 30
?
I know it has to be something with:
difference_height = height - next_height
difference_weight = weight - next_weight
But I don't know how to modify it.