Search code examples
pythonwhile-loopbreak

Why is a test (100/100) leading to an unexpected output?


I am trying to learn python. In this context, I work on this exercise:

  • I take a fraction from the user (X/Y) and return a result
  • If Y is greater than X, I prompt the user to provide another fraction
  • If X or X are not integers, I prompt the user to provide another fraction
  • If X is greater than Y, I prompt the user to provide another fraction
  • If the fraction leads to a result over 99%, I print F
  • If the fraction leads to a result below 1%, I print E

My code is as follows:

z = input("Fraction: ")
k = z.split("/")

while True:
    try:
        x = int(k[0])
        y = int(k[1])
        if y >= x:
           result = round((x / y) * 100)
        else:
           z = input("Fraction: ")
# if x and y are integer and y greater or equal to x, then divide, and round x and y

    except (ValueError, ZeroDivisionError):
        z = input("Fraction: ")

# if x and y are not integer or y is zero, prompt the user again

    else:
        break

# exit the loop if the condition is met and print (either F, E or the result of the division)

if result >= 99:
    print("F")
elif 0 <= result <= 1:
    print("E")
else:
    print(f"{result}%")

The input of 100/100 leads to another prompt, when it should lead to F as an output.

I do not understand why.


Solution

  • Alright so the problem stems from your placement of the K variable. While you are assigning input to K initially, if for whatever reason the first input were to throw the zero division error or a value error, the program doesn't work as expected because you never reevaluate the input hence just moving your k variable into your try clause, or more accurately into the while loop, should fix the issue:

    z = input("Fraction: ")
    while True:
        try:
            k = z.split("/")
            x = int(k[0])
            y = int(k[1])
            if y >= x:
               result = round((x / y) * 100)
            else:
               z = input("Fraction: ")
               continue
    # if x and y are integer and y greater or equal to x, then divide, and round x and y
    
        except (ValueError, ZeroDivisionError):
            z = input("Fraction: ")
    
    # if x and y are not integer or y is zero, prompt the user again
    
        else:
            break
    
    # exit the loop if the condition is met and print (either F, E or the result of the division)
    
    if result >= 99:
        print("F")
    elif 0 <= result <= 1:
        print("E")
    else:
        print(f"{result}%")
    

    As an added note, be more clear in your initial question and ensure you provide all of the inputs you have made, the best way to ensure you provide us with all of your inputs, rerun your program and make note of any inputs then on including blank inputs in problems like this.

    Edit: Added a continue in the case that the y < x which would result in another input being required hence the result wouldn't be defined.