Python Forum
ZeroDivisionError help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ZeroDivisionError help
#9
If you enter a number like 060 that is no problem for the int() function:

num = '060'
int(num) # returns 60
In any division, it is OK if the dividend is zero, so we do not need to check num1 at all.

In any division, the divisor should not be zero, because mathematicians say, this division is "undefined". I disagree with that formulation, but that is another matter.

The string 0 (zero) starts with zero, so to save trouble, and because we do not normally write leading zeroes, we will just reject any input string for num2 which begins with zero:

import re

e = re.compile('0')
f = re.compile(r'\D')
# keep asking for numbers until the divisor does not start with zero
# only integers allowed
def do_division():
    while True:
        print('Attention attention!')
        print('Only integers allowed, no other characters.')
        print('The first integer can be zero, the second integer camnnot start with zero!')
        nums = input('Enter your 2 integers, separated by a space ... ').split()
        # check for any non-number characters
        if f.search(nums[0]) or f.search(nums[1]):
            print('Only integers allowed, no other characters, try again ... ')
        # check for 0 at the start of the string
        elif e.match(nums[1]):
            print('The divisor cannot start with zero, try again ...')
        # if the above are good get the result    
        else:
            result = int(nums[0]) / int(nums[1])
            print(f'{int(nums[0])} / {int(nums[1])} = {result}')
            break
    return result
e.match(nums[1]) looks to see if nums[1] begins with zero. (Zero also begins with zero.)

f.search(n) looks to see if nums[0] or nums[1] contain any characters that are not numbers (that's the \D)

If e and f both return None, the division can go ahead.

Thinking about what to do with floats, I think the whole thing is better like this, for floats or integers:

import re

# find a float or integer
# find a float
g = re.compile(r'\A(?=\d+(.\d+)?\Z).*')
# use h to look for at least 1 number between 1 and 9 in the divisor
# otherwise, there are just zeroes
h = re.compile(r'[1-9]+')
def do_division():
    while True:
        print('Attention attention!')
        print('Only integers or floats allowed: number or number.number, no other characters.')
        nums = input('Enter your numbers, separated by a space ... ').split()        
        if not h.search(nums[1]):
            print('And the second number must contain at least 1 number > 0 ... ')
            continue        
        # check for any non-number characters
        elif g.match(nums[0]) and g.match(nums[1]):
            result = float(nums[0]) / float(nums[1])
            print(f'{nums[0]} / {nums[1]} = {result}')
            break
        else:
            print('The numbers you entered do not have the correct format, try again ... ') 
    return result
\A the start or the string
\d+ one or more digits
(.\d+)? optionally followed by .digit(s) like .2345
\Z end of string

Anything else will not return a match.
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