Robo found a mystery fruit on the floor. It looks partly Red and partly Yellow.
Is it an Apple or a Banana?
To decide, Robo measures the Distance. Who is it closer to?
In math, distance is just the Difference. We use abs() (Absolute Value) so it's always positive.
Who is the "Nearest Neighbor"? The one with the Smallest Distance!
Define the fruits by size.
Apple=10, Banana=20.
The mystery fruit is size 12.
apple = 10 banana = 20 mystery = 12
Calculate distance to apple (`a_dist`) and banana (`b_dist`). Tips: Use abs(a - b).
a_dist = abs(apple - mystery) b_dist = abs(banana - mystery) print(a_dist) print(b_dist)
Write an if statement. If `a_dist` is smaller (`<`) than `b_dist`, it's an Apple!
if a_dist < b_dist:
print("It is an Apple!")
else:
print("It is a Banana!")
1. If Apple is at 10 and Mystery is at 100, is the distance small or big?