Regular Expression Issue: Program Not Finding 5 or more Digits
Question:
I’m working on freeCodeCamp’s arithmetic formatter. I have to show four errors in my code, they are shown in the comments below. I have to check the inputs to make sure there are no more than 4 digits in each operand. I am trying to use ‘bd{5, }b’ to accomplish this, but it will not work. I entered it into the regex101 website, and it is highlighting the correct operand (1222243) in the last input. But when I run my program the terminal displays "none" instead of the error. Can you help me figure out what I did wrong?
import re
def arithmetic_arranger(problems):
for problem in problems:
if len(problems) > 5:
return "Error: Too many problems."
elif re.search("[/]", problem) or re.search("[*]", problem):
return "Error: Operator must be '+' or '-'."
elif re.search("[a-z]", problem):
return "Error: Numbers must only contain digits."
elif re.search('bd{5, }b', problem):
return "Error: Numbers cannot be more than four digits."
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 / 49"])) #Error: Operator must be '+' or '-'.
print(arithmetic_arranger(["52 + 128", "7601 - 3", "47 + 903", "178 / 88","45 + 43", "77 + 13" ])) #Error: Too many problems.
print(arithmetic_arranger(["32 + 698", "3801 - 72", "95 + 43", "123 + 19" ])) #none
print(arithmetic_arranger(["99 + 612", "4561 - 344", "21 + 43", "1f23 + 566" ])) #Error: Numbers must only contain digits.
print(arithmetic_arranger(["50 + 233", "1234 - 14", "79 + 1222243", "111 + 60" ])) #Error: Numbers cannot be more than four digits.
Answers:
Try this:
# import operator
def arithmetic_arranger(problems):
if len(problems) > 5:
return "Error: Too many problems."
results = []
for problem in problems:
num1, op, num2 = problem.split()
if op == '/' or op == '*':
return "Error: Operator must be '+' or '-'."
if not num1.isdigit() or not num2.isdigit():
return "Error: Numbers must only contain digits."
if len(num1) > 4 or len(num2) > 4:
return "Error: Numbers cannot be more than four digits."
n1, n2 = int(num1), int(num2)
answer = (n1 + n2) if op == '+' else (n1 - n2)
results.append('{} = {}'.format(problem, answer))
return results
Your pattern shouldn’t have a space where it does – try r'bd{5,}b'
.
I’m working on freeCodeCamp’s arithmetic formatter. I have to show four errors in my code, they are shown in the comments below. I have to check the inputs to make sure there are no more than 4 digits in each operand. I am trying to use ‘bd{5, }b’ to accomplish this, but it will not work. I entered it into the regex101 website, and it is highlighting the correct operand (1222243) in the last input. But when I run my program the terminal displays "none" instead of the error. Can you help me figure out what I did wrong?
import re
def arithmetic_arranger(problems):
for problem in problems:
if len(problems) > 5:
return "Error: Too many problems."
elif re.search("[/]", problem) or re.search("[*]", problem):
return "Error: Operator must be '+' or '-'."
elif re.search("[a-z]", problem):
return "Error: Numbers must only contain digits."
elif re.search('bd{5, }b', problem):
return "Error: Numbers cannot be more than four digits."
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 / 49"])) #Error: Operator must be '+' or '-'.
print(arithmetic_arranger(["52 + 128", "7601 - 3", "47 + 903", "178 / 88","45 + 43", "77 + 13" ])) #Error: Too many problems.
print(arithmetic_arranger(["32 + 698", "3801 - 72", "95 + 43", "123 + 19" ])) #none
print(arithmetic_arranger(["99 + 612", "4561 - 344", "21 + 43", "1f23 + 566" ])) #Error: Numbers must only contain digits.
print(arithmetic_arranger(["50 + 233", "1234 - 14", "79 + 1222243", "111 + 60" ])) #Error: Numbers cannot be more than four digits.
Try this:
# import operator
def arithmetic_arranger(problems):
if len(problems) > 5:
return "Error: Too many problems."
results = []
for problem in problems:
num1, op, num2 = problem.split()
if op == '/' or op == '*':
return "Error: Operator must be '+' or '-'."
if not num1.isdigit() or not num2.isdigit():
return "Error: Numbers must only contain digits."
if len(num1) > 4 or len(num2) > 4:
return "Error: Numbers cannot be more than four digits."
n1, n2 = int(num1), int(num2)
answer = (n1 + n2) if op == '+' else (n1 - n2)
results.append('{} = {}'.format(problem, answer))
return results
Your pattern shouldn’t have a space where it does – try r'bd{5,}b'
.