Python Forum
WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! (/thread-40824.html)



WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! - ayodele_martins1 - Sep-30-2023

#WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP!
# Using the Modulus operator to find the reminder of any two operands
while True:
    try:
        divisor = int(input("Enter divisor operand: "))
        break
    except ValueError as e:
        print(f"{e}; is not an acceptable value ")
    

def modulus(dividend):
        while True:
            try:
                remainder = dividend % divisor
                qoutient = dividend // divisor
                if remainder == 0:
                    return f"Operand {dividend} has {remainder} remainder after division by {divisor}, with {qoutient} as the qoutient!"
                else:
                    return f"Operand {dividend} has a reminder of {remainder} after division by {divisor} with {qoutient} as the qoutient !"
            except ZeroDivisionError as e:
                    return f"{e}:not divisble"


while True:
    try:
        dividend = int(input("Enter dividend operand to get remainder: "))
        break
    except ValueError as e:
        print(f"{e}; not a valid input")
        
result = modulus(dividend)
print(result)



RE: WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! - ayodele_martins1 - Sep-30-2023

Thank You @deanhystad will do.


RE: WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! - ayodele_martins1 - Sep-30-2023

#WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP!
# Using the Modulus operator to find the reminder of any two operands
while True:
    try:
        divisor = int(input("Enter divisor operand: "))
        break
    except ValueError as e:
        print(f"{e}; is not an acceptable value ")

def modulus(dividend):
    while True:
        try:
            remainder = dividend % divisor
            qoutient = dividend // divisor
            if remainder == 0:
                return f"Operand {dividend} has {remainder} remainder after division by {divisor}, with {qoutient} as the qoutient!"
            else:
                print( f"Operand {dividend} has a reminder of {remainder} after division by {divisor} with {qoutient} as the qoutient !" )
        except ZeroDivisionError as e:
            print(f"{e}: {divisor} not divisble by {dividend}")
            break
        
            
while True:
    try:
        dividend = int(input("Enter dividend operand to get remainder: "))
        break
    except ValueError as e:
        print(f"{e}; not a valid input")
        

result = modulus(dividend)
print(result)
Output:
Enter divisor operand: 0 Enter dividend operand to get remainder: 2 integer modulo by zero: 0 not divisble by 2



RE: WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! - deanhystad - Sep-30-2023

I don't understand your modulus function. Why is there a while loop? No matter what you enter, the code returns a value the first time through the loop.

I don't understand your question. If you enter zero for the divisor, the modulus function returns "integer modulo by zero:not divisble". "integer module by zero" is the message returned when you tried modulo division with 0 as the divisor. ":not divisible" is the part you added on. What is your question?

If you want quotient and remainder, take a look at divmod().

The convention for functions is to place them at the top of the file, with any body code at the bottom of the file.


RE: WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! - ayodele_martins1 - Sep-30-2023

My question:

Why is the loop not returning the user back to the input after the ZerroDivisionError. Just like the ValueError which returns the input after the user input wrong value.
Hope you get my question now?

Every thing works fine except the loop not returning the input for users after the ZerroDivisionError which just breaks out.


RE: WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! - deanhystad - Sep-30-2023

The function doesn't loop because you return a value:
            except ZeroDivisionError as e:
                    return f"{e}:not divisble"  # Returns out of the function
And in your newer code you break out of the loop.
        except ZeroDivisionError as e:
            print(f"{e}: {divisor} not divisble by {dividend}")
            break  # This ends the loop
Your newer code will loop forever if you have a remainder.
            else:
                print( f"Operand {dividend} has a reminder of {remainder} after division by {divisor} with {qoutient} as the qoutient !" )
You should not have a loop inside the modulus() function. It would only make sense to have a loop if there was a way for the user to enter a different divisor. The loop belongs before the modulus() function is called. I would write a function like this:
def input_int(prompt):
    while True:
        try:
            inp = input(prompt)
            return int(inp)
        except ValueError as msg:
            print(msg)
This is a useful function that can be reused to input the dividend and the divisor.
dividend = input_int("Enter integer dividend: ")
while True:
    if divisor := input_int("Enter integer divisor: ")
        break
    print("Divisor cannot be zero.")



RE: WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! - ayodele_martins1 - Oct-01-2023

Thank You Sooo much deanhystad. I will feedback after trying the code.


RE: WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! - ayodele_martins1 - Oct-01-2023

[inline]

Hello deanhystad, thanks a bunchhh the code you suggested worked fine for all wrong inputs!!! Grateful.
Just learning Python, so getting my hands on writing codes related to real life problems.

[/inline]


def input_int(prompt):
    while True:
        try:
            inp = input(prompt)
            return int(inp)
        except ValueError as msg:
            print(msg)

dividend = input_int("Enter integer dividend: ")
while True:
    if divisor := input_int("Enter integer divisor: "):
        break
    print("Divisor cannot be zero.")    

def modulus(dividend):
    rem = dividend % divisor
    qout= dividend // divisor
    return (f"{dividend} modulo {divisor} has a remainder of {rem}")

print(modulus(dividend))
Output:
Enter integer dividend: 8 Enter integer divisor: 0 Divisor cannot be zero. Enter integer divisor: 2 8 modulo 2 has a remainder of 0