0

I am doing the Problem set 3 :Fuel Gauge. With every other fraction input the program works fine, but when I inputed 10/10, the program will can't detect /, and so that the program can't split the fraction successfully, the terminal will show not containing /. I just didn't get it, did I overlook any failure?

import string

def main():

while True:

    s = input("Fraction: ")

    number = []




    if s.find("/") != True:
        print("not containing /")
        pass

    else:
        number = s.split('/')
        x = number[0]
        y = number[1]
        if "." in x or "." in y:
            pass

        elif x.isdigit() != True or y.isdigit() != True :
            print("contain . ")
            pass

        else:
            printN(x,y)
            break

def printN(x,y):

try:
    x_f = float(x)
    y_f = float(y)
    result = x_f/y_f*100
except (ValueError, ZeroDivisionError):
    pass


else:

    while True:
        if   result >= 99:
            print("F")
            break
        elif result <= 1:
            print("E")
            break
        elif result >= 101:
            pass
        else:
            print(round(result),"%",sep='')
            break

main()

1 Answer 1

0

Read the doc on pthon string find method:

Return the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

Note

The find() method should be used only if you need to know the position of sub. To check if sub is a substring or not, use the in operator:

Methinks this code only works if the "/" is in index 1 of the string s (eg 2/3).

1
  • yes your are right, If I type in double digit numerator, then the program can't detect '/' Commented Oct 10, 2022 at 23:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .