An active title states the finding. Generate one from the first and last values of a series.
What to build
A program that writes a chart title stating the change.
Requirements
- Print
METRIC rose P% from L1 to L2orMETRIC fell P% from L1 to L2, with P the size of the percentage change rounded to a whole number, orMETRIC were unchanged from L1 to L2when the values are equal.
Input
Line 1: the metric name. Line 2: L1 V1. Line 3: L2 V2.
Output
One line.
Constraints
Values are over 0; labels and the metric are single words.
Example
Input:
Sales
Jan 100
Feb 120
Output:
Sales rose 20% from Jan to Feb
Why it works
The title turns two numbers into the sentence a reader needs, and the relative change makes the size of the difference meaningful whatever the units.
Examples
Example 1 · Input
Sales Jan 100 Feb 120
Expected output
Sales rose 20% from Jan to Feb
Example 2 · Input
Visits Mon 80 Tue 60
Expected output
Visits fell 25% from Mon to Tue
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The change in percent is (v2 − v1) ÷ v1 × 100.
Hint 2
Print its size without a sign, rounded to a whole number, and choose rose or fell from the sign.
Solution
Show one correct solution (try it yourself first)
metric = input()
l1, v1 = input().split()
l2, v2 = input().split()
a, b = float(v1), float(v2)
if a == b:
print(f"{metric} were unchanged from {l1} to {l2}")
else:
p = abs(b - a) / a * 100
print(f"{metric} {'rose' if b > a else 'fell'} {p:.0f}% from {l1} to {l2}")