mina Zarief
mina Zarief

Reputation: 3

Python code percentage number compare problem

when i compared tow percentage number it gives me wrong answer

a="{0:%}".format(85/100)
b="{0:%}".format(9/100)

if b>a :
    print("done")

it should be pass if condition while its give me done in answer

Upvotes: 0

Views: 285

Answers (1)

J_H
J_H

Reputation: 20460

when i compare two percentage numbers it gives me wrong answer

No, the interpreter gives the correct answer.

The variables get these string values:

a = '85.000000%'
b = '9.000000%'

You are complaining about this string comparison result:

>>> '9' > '85'
True

Or more simply, since they differ in the 1st character, about this result:

>>> '9' > '8'
True

If you would prefer a numeric comparison, then strip the percent and recover the numbers:

>>> float(b[:-1]) > float(a[:-1])
False

Upvotes: 1

Related Questions