Arithmetic Formatter Python error help!

I got this error and I’m not sure what it means:

python main.py
Traceback (most recent call last):
File “main.py”, line 7, in
print(arithmetic_arranger([“32 + 698”, “3801 - 2”, “45 + 43”, “123 + 49”]))
File “/home/runner/boilerplate-arithmetic-formatter/arithmetic_arranger.py”, line 16, in arithmetic_arranger
elif re.search(“*”, every) or re.search(“/”, every):
File “/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/re.py”, line 201, in search
return _compile(pattern, flags).search(string)
File “/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/re.py”, line 304, in _compile
p = sre_compile.compile(pattern, flags)
File “/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/sre_compile.py”, line 764, in compile
p = sre_parse.parse(p, flags)
File “/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/sre_parse.py”, line 948, in parse
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
File “/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/sre_parse.py”, line 443, in _parse_sub
itemsappend(_parse(source, state, verbose, nested + 1,
File “/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/sre_parse.py”, line 668, in _parse
raise source.error(“nothing to repeat”,
re.error: nothing to repeat at position 0
exit status 1

Your code so far
https://replit.com/@jpham658/boilerplate-arithmetic-formatter#arithmetic_arranger.py

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

"*" is not a valid regular expression. * means “0 or more”, but 0 or more what? * should follow some other character or group.

Maybe you were expecting it work like a wildcard?

oh i was trying to make it look for the character “*” because that’s what people tend to use to indicate multiplication, should i have put a backslash in front of it so it recognises it as a character?

Yes \* would match the literal *

hi, i replaced it and it still didn’t work. here’s what i wrote:

elif re.search("\*", every) or re.search("/", every):
  return("Error: Operators must be '+' or '-'.")

then i tried using:

try:
  problems.index("*")
  problems.index("/")
  return("Error: Operators must be '+' or '-'.")
except:
  pass

but nothing is working and i can’t find anything online :smiling_face_with_tear:!!! any help would be appreciated

Try using a regex set like this

re.search([*],every)
elif re.search([*], every) or re.search([/], every):
  return("Error: Operators must be '+' or '-'.")

this gave an invalid syntax error

elif re.search(["\*"], every) or re.search(["/"], every):
  return("Error: Operators must be '+' or '-'.")

and this failed the test but ran

Sorry, try again but put the pattern inside the double quotes “[*]”
(I missed typing them the first time)

hi, thanks for the reply! i put “*” into the square bracket and it still fails the test. this is the whole bit of code i’ve written for input correcting:

for every in problems:
  if re.search("^\D+$", every)==False:  
    return("Error: Numbers must only contain digits.")
  elif len(every) > 4:
    return("Error: Numbers cannot be more than four digits.")
  elif re.search(["*"], every) or re.search(["/"], every):
    return("Error: Operators must be '+' or '-'.")

(i already know there’s something wrong with the digit checker but i just can’t figure it out, i will try to think about it a little bit more)

The pattern should be in double quotes like this

”[*]”

Edit: for the digit checking, probably you need a regex that checks for anything that is NOT a digit. (And if you get a match return error). So something like

re.search(“[^0-9]”)
elif re.search("[*]", every) or re.search("[/]", every):
  return("Error: Operators must be '+' or '-'.")

i’ve done this and it’s still failed the test :fearful:

What was the text in this case?
(Try to print the value inside of every before you do the search)

cos it’s the repl.it that the fcc course created, i’m not sure i can print it cos they have their own tests and it just gets jumbled up in the entire output from the tests

the arguments for the operator tests are below:

arguments = [['3 / 855', '3801 - 2', '45 + 43', '123 + 49']]

tbh, i have no idea what the test output is saying, so i’ll just paste it here cos i’m sure that’s the operator test:

arguments = [['3 / 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = "Error: Operator must be '+' or '-'."
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be \'+\' or \'-\'."'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."
E       assert 'Error: Numbers cannot be more than four digits.' == "Error: Operator must be '+' or '-'."
E         - Error: Operator must be '+' or '-'.
E         + Error: Numbers cannot be more than four digits.

Thanks for sending that (but pls use the reply to post button if you need me specifically to get notification).

The error is saying that it was expecting you to catch the division symbol but instead the program said there are too many digits. (Read the last two lines to see that)

Knowing this, do you think the previous if statement is correct? (The one checking the len)

Also, you can edit the main program that is calling the test. I personally commented the test out so I can focus on fixing my code first. (Just remember to undo the comment later)

ah, right, i see. i’ll check over it again and see if i can make any progress then. thank you so much for all the help! :smiling_face:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.