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
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%]
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
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?
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=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:
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.