Alfons
Alfons

Reputation: 15

Check how close one integer is to another

I have a target value 10.

A new input value is 9.

9 matches 10 to 90%.

10 would match 10 to 100 %

0 would match 10 to 0%

and so on.

But it is much harder to calculate manually if the target value it's let's say 676 and the input value 234.

How do I write a script that, given a target and a input value, returns me that percentage?

Upvotes: 0

Views: 60

Answers (1)

Corentin Pane
Corentin Pane

Reputation: 4943

What you're looking for is the ratio between the input and the target. You can display it this way:

target = 676
input = 234

print(100 * input/target, "%")

It will throw an exception if the target is 0 of course.

Upvotes: 1

Related Questions