Scientific Computing with Python Projects - Arithmetic Formatter

Hello,
I have completed the Arithmetic Formatter project but when I run it it gives me an error; /home/runner/boilerplate-arithmetic-formatter/venv/bin/python3: line 3: exec: : not found. I think its referencing the “from pytest import main” in main.py. It had previously given me an error; “pytest not found”. I hope it is just this and not a problem with my code. Please help me with what the problem is really so I can continue with the test.

**I read that you have to remove and reinstall pytest in the boilerplater-arithmetic-formatter repo but I’m not sure how to do this. When I type; pip install pytest, in the shell command I get; /home/runner/boilerplate-arithmetic-formatter/venv/bin/python3: line 3: exec: : not found.

Your code so far
def arithmetic_arranger(problems, cond = False):
if len(problems) > 5:
return(“Error: Too many problems.”)

foper = list()
soper = list()
oper = list()
rslt = list()
fline = str()
sline = str()
tline = str()
rline = str()

#Splitting the operands
for problem in problems:
  problem.lstrip()
  problem.rstrip()
  sproblem = problem.split()
  if len(sproblem[0] > 4 or sproblem[2] > 4):
    return("Error: Numbers cannot be more than four digits")
  if not (sproblem[1] == '+' or sproblem[1] == '-'):
    return("Error: Operator must be '+' or '-'.")
  foper.append(sproblem[0])
  oper.append(sproblem[1])
  soper.append(sproblem[2])

#The arithmetic operations
counter = 0
while counter < len(problems):
  try:
    x = int(foper[counter])
    y = int(soper[counter])
  except:
    return("Error: Numbers must only contain digits")
  if oper[counter] == '+':
    rslt.append(str(x + y))
  elif oper[counter] == '-':
    rslt.append(str(x - y))

#finding length of longest operand
length = max(len(foper[counter]), len(soper[counter]))

#first line
for val in range((length + 2) - len(foper[counter])):
  foper[counter] = ' ' + foper[counter]
fline = fline + foper[counter] + '    '

#second line
for val in range((length + 1) - len(soper[counter])):
  soper[counter] = ' ' + soper[counter]
sline = sline + oper[counter] + soper[counter] + '    '

#third line
for val in range(length + 2):
  tline = tline + '-' 
tline = tline + '    '

#result line
for val in range((length + 2) - len(rslt[counter])):
  rslt[counter] = ' ' + rslt[counter]
rline = rline + rslt[counter] + '    '

counter = counter + 1

#final output
fline = fline.rstrip()
sline = sline.rstrip()
tline = tline.rstrip()
rline = rline.rstrip()
if cond == False:
   arranged_problems = fline + '\n' + sline + '\n' + tline
elif cond == True:
   arranged_problems = fline + '\n' + sline + '\n' + tline +  '\n' + rline

return arranged_problems

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

You can try to fork your replit so it copies everything into a new environment, maybe something is corrupted.

Can you provide a link to your replit?

Sure,

Something is jacked, when I forked your replit it had the same problem.

I imported a new replit from the original boilerplate and copied just the code from your arithmetic_arranger.py file and it ran properly.

Here it is with your code if you want to fork that: boilerplate-arithmetic-formatter (1) - Replit

1 Like

Hi, again

So I just ran the code and it comes back with 9 failures and 1 pass. Does this mean there is something wrong with my code?

Im using the repl I forked from yours. Here is a list of the errors;
test_module.py::test_template[test_two_problems_arrangement1] FAILED [ 10%]
test_module.py::test_template[test_two_problems_arrangement2] FAILED [ 20%]
test_module.py::test_template[test_four_problems_arrangement] FAILED [ 30%]
test_module.py::test_template[test_five_problems_arrangement] FAILED [ 40%]
test_module.py::test_template[test_too_many_problems] PASSED [ 50%]
test_module.py::test_template[test_incorrect_operator] FAILED [ 60%]
test_module.py::test_template[test_too_many_digits] FAILED [ 70%]
test_module.py::test_template[test_only_digits] FAILED [ 80%]
test_module.py::test_template[test_two_problems_with_solutions] FAILED [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] FAILED [100%]

Thank you.

Yes, it does. Check out the more verbose output, it will tell you exactly what’s wrong:

E       assert None == '  3801      123\n-    2    +  49\n------    -----'

This is saying your function is returning “None” but it expects ’ 3801 123\n- 2 + 49\n------ -----'.

If I comment out the tests from the main.py and just test the sample print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])) my output is also None

If I change your final output like this:

print("Made it here", arranged_problems)
return arranged_problems

It doesn’t print, it never makes it to that line.

It was an indentation error from the copy/paste. I fixed it now getting this error:

File "/home/runner/boilerplate-arithmetic-formatter-1/arithmetic_arranger.py", line 19, in arithmetic_arranger
    if len(sproblem[0] > 4 or sproblem[2] > 4):
TypeError: '>' not supported between instances of 'str' and 'int'

Here is line 19:
if len(sproblem[0] > 4 or sproblem[2] > 4):

I’ve fixed the the len() problem but it still returns none. I’m not sure what is wrong with my code. If you can could you please take a look at it and give me some pointers?

I cant seem to get this code to produce the desired output. I am not sure what is wrong but it keeps saying the tests have failed. I have been stuck on this project for over a month now and have changed my code three or four times but I keep getting the same results.

Can someone please help with this project. What am I doing wrong?

This is my replit link: boilerplate-arithmetic-formatter (1) - Replit

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

+ 3801    -    2    ------
+  123    +   49    ------
-   3801      123
- -    2    +  49
- ------    -----

The green is what you are producing and the red is what is expected.

Your output is not what is expected.

How do I get the dashes to line up with my numbers. Ive been trying this for a while and I am completely confused.

That’s a great question. I would make each of the three required rows separately and then put them together once you have added all problems to each row.

I like to use this part of the error message for this problem, because it shows you exactly the kind of string you are trying to build:

E   AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E   assert '3801    -    2    ------\n 123    +   49    ------' == '  3801      123\n-    2    +  49\n------    -----'

Here is your raw string output:

‘3801 - 2 ------\n 123 + 49 ------’

You can see here you are printing a problem all together, then the dashes, then a newline. Then the second problem.

and here is the string you need to build:

’ 3801 123\n- 2 + 49\n------ -----’

Imagine a printer going line by line to print two problems next to each other.

It really helped me to print this:

print(repr(arranged_problems))

This will show you the raw string output as it appears above. Makes it very clear what’s happening.

Thats what I thought I did here:

Unless I made a mistake. Could you give me pointers?

You are making a separate row for each problem, which is backwards.

1 Like

Follow the logic of that loop.

i=0 will complete each line1, 2 and 3 for the first problem.
i=1 will complete each line1, 2 and 3 for the second problem.

Look at your output:
print(repr(arranged_problems))

If you are building a string one element at a time, the first element will be first_numbers[0]. The second element will be first_numbers[1]. Third element will be a newline. Fourth element will be operators[0].

The string will look like this so far:

’ 3801 123\n-’

Then you’ll need to add second_numbers[0], operators[1] and second_numbers[1] and a newline:

’ 3801 123\n- 2 + 49\n’

Thanks I think I have fixed for that now but there’s a little bug that I don’t really understand in the code:

elif len(problems) == 4:                     
       line1 = arranged_problems.append(f"  {first_numbers[0]:>{max_width}}"   
    f"{first_numbers[1]:>{max_width}}"
    f"{first_numbers[2]:>{max_width}}"
    f"{first_numbers[3]:>{max_width}}\n")
  line2 = arranged_problems.append(f"  {operators[0]}"
    f"{second_numbers[0]:>{max_width}}"        f"{operators[2]}"
    f"{second_numbers[2]:>{max_width}}"
    f"{operators[3]}"
    f"{second_numbers[3]:>{max_width}}\n")
  line3 = arranged_problems.append('-' * (max_width + 2))
                          
  elif len(problems) == 5:              
      line1 = arranged_problems.append(f"  {first_numbers[0]:>{max_width}}"
    f"{first_numbers[1]:>{max_width}}"
    f"{first_numbers[2]:>{max_width}}"
    f"{first_numbers[3]:>{max_width}}"
    f"{first_numbers[4]:>{max_width}}\n")

In the line elif len(problems)==5: it gives me a syntax error when I run it. Im not sure how this is possible. I’ve run it on replit and IDLE and it gives me the same syntax error.

EDIT: Here you can see what it looks like when you nest your code in triple backticks

Can you share a full, exact copy/paste of the error?

Try to also include your full code, at least the full if statement, and enclose it using triple backticks (use the code/preformatted text button in the menu at the top of the editor that looks like </> if you want). That will preserve indenations and formatting.

That said this code is very repetitive and it would be better if you can write a single loop that will work for any number of problems.

I dont see that

Also I thought about doing a loop but I want to know if this works first before doing anything else.