How to enter a fraction with two digits in denominator

67 Views Asked by At

Does anyone know a way I can put in 9/10 or similar without getting the "Not a real value text"?
It works fine with single digits like 3/4, 2/5 but if I put in 2/10 I get the error.

while True:
    numerator, denominator = input("What is fraction is your fuel ").split("/")
    if numerator > denominator:
        print("Not real value, try again ")
        continue
    elif .90<= int(numerator)/int(denominator) <=.99:
        print("f")
    elif 0<= int(numerator)/int(denominator) <.10:
        print("E")
    else:
        fuel = str(round(int(numerator) / int(denominator) *100))
        print("Fuel meter is at: " + fuel + "%")
        break
1

There are 1 best solutions below

2
juanpethes On

The reason it doesn't work for 9/10 is because num and dem are stored as strings, and when you use < and > to compare strings, it compares them alphabetically. So it correctly knows 9 > 8 > 7 > ... but it thinks 9 > 11 > 10 > ... etc because it as mentioned does it alphabetically. To fix this, convert num and dem to integers, like so:

num, dem = input("What is fraction is your fuel ").split("/")
num = int(num)
dem = int(dem)
...