Python Forum
ZeroDivisionError help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ZeroDivisionError help
#8
I think the way to do this is remove input validation from the test and put it in a special function that only does input. Something like this:
def input_number(prompt, tests=None):
    """Get integer input.  Verify input passes all tests."""
    while True:
        try:
            value = int(input(prompt))
            if tests is None or all(test(value) for test in tests):
                return value
        except ValueError:
            pass


a = input_number("Enter an integer : ")
b = input_number("Enter a non-zero integer : ", (lambda x: x != 0,))
print(f"{a} / {b} = {a / b}")

a = input_number("Enter an even integer > 8 : ", (lambda x: x % 2 == 0, lambda x: x > 8))
b = input_number("Enter an odd integer : ", (lambda x: x % 2 != 0,))
print(f"{a} * {b} = {a * b}")
Jeff_900 likes this post
Reply


Messages In This Thread
ZeroDivisionError help - by zimmytheflygirl - Jun-18-2024, 05:57 PM
RE: ZeroDivisionError help - by Jeff_900 - Jun-18-2024, 07:17 PM
RE: ZeroDivisionError help - by zimmytheflygirl - Jun-18-2024, 07:22 PM
RE: ZeroDivisionError help - by Jeff_900 - Jun-18-2024, 07:30 PM
RE: ZeroDivisionError help - by zimmytheflygirl - Jun-18-2024, 07:45 PM
RE: ZeroDivisionError help - by zimmytheflygirl - Jun-18-2024, 07:56 PM
RE: ZeroDivisionError help - by Jeff_900 - Jun-18-2024, 08:00 PM
RE: ZeroDivisionError help - by deanhystad - Jun-19-2024, 12:43 AM
RE: ZeroDivisionError help - by Pedroski55 - Jun-21-2024, 03:14 PM
RE: ZeroDivisionError help - by zimmytheflygirl - Jun-21-2024, 11:58 PM
RE: ZeroDivisionError help - by deanhystad - Jun-22-2024, 03:22 AM
RE: ZeroDivisionError help - by zimmytheflygirl - Jun-24-2024, 01:15 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020