Jun-21-2024, 03:14 PM
(This post was last modified: Jun-21-2024, 03:14 PM by Pedroski55.)
If you enter a number like 060 that is no problem for the int() function:
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:
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:
\d+ one or more digits
(.\d+)? optionally followed by .digit(s) like .2345
\Z end of string
Anything else will not return a match.
num = '060' int(num) # returns 60In 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 resulte.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.