Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Hi,

I wouldn’t ask unless I hadn’t racked my brains for hours.
I’ve just finished the video course and spend the last few months doing exercise after exercise of for loops, regex, dictionaries, etc., and felt ready enough to try the course projects.

I’ve spent the last few days writing a code, slowly trying to fulfill each requirement. Although fairly tricky for someone who only started coding this year, I managed most of it easily enough apart from the layout to be returned from the function - that was tough!

Anyway, I can get the screen to print out a result if I don’t format the result to the requirements of the task, but when I run my final code, it doesn’t tell me there are any errors, it just doesn’t display anything. I’ve tried in several interpreters, but for some reason, I can’t get any result to display when I run the code.

I also have to call the function myself because I can’t get Replit to test the code for me against the test_module.py, for some reason. Likewise for that, I’ve tried many things, but can’t seem to get Replit to do that for me either…

I hope this brings nostalgia to some of you who’ve been coding for a while and remember going through this during your first months or year while learning… I don’t want to give up, but it’s frustrating…

If anyone could help, you’d be a lifesaver! I imagine it must be one or two small problems, but I can’t for the life of me see what it must be…

Your code so far

import re

def arithmetic_arranger(problems, cond = False):

``````firstline = ""
secondline = ""
dividingline = ""
resultsum = ""
blockofsums = ""

for i in problems:
if len(problems) > 4:
print("Error: Too many problems")
break
k = re.split(r'\s+', i)
firstnum = k[0]
secondnum = k[2]
operator = k[1]
a = int(firstnum)
b = int(secondnum)
if "*" in k:
print("Error: Operator must be '+' or '-'.")
break
elif "/" in k:
print("Error: Operator must be '+' or '-'.")
break
for j in k:
if len(j) > 4:
print("Error: Numbers cannot be more than four digits.")
break

if operator == "+":
sum = str(a + b)
elif operator == "-":
sum = str(a - b)

length = max(len(firstnum), len(secondnum)) + 2
topline = str(firstnum).rjust(length)
bottomline = operator + str(secondnum).rjust(length - 1)
line = ""
result = str(sum).rjust(length)

if i != problems[-1]:
firstline += topline + '    '
secondline += bottomline + '    '
dividingline += line + '    '
resultsum += result + '    '
else:
firstline += topline
secondline += bottomline
dividingline += line
resultsum += result

if cond:
blockofsums = firstline + "\n" + secondline + "\n" + dividingline + "\n" + resultsum
else:
blockofsums = firstline + "\n" + secondline + "\n" + dividingline

return blockofsums
``````

arithmetic_arranger([“231 + 800”, “34 + 6415”, “308 + 8”, “4 + 3”], True)

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

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

I’ve also tried editing the indentations, as follows below, but it still won’t show a result…

import re

def arithmetic_arranger(problems, cond = False):

``````firstline = ""
secondline = ""
dividingline = ""
resultsum = ""
blockofsums = ""

for i in problems:
if len(problems) > 4:
print("Error: Too many problems")
break
k = re.split(r'\s+', i)
firstnum = k[0]
secondnum = k[2]
operator = k[1]
a = int(firstnum)
b = int(secondnum)
if "*" in k:
print("Error: Operator must be '+' or '-'.")
break
elif "/" in k:
print("Error: Operator must be '+' or '-'.")
break
for j in k:
if len(j) > 4:
print("Error: Numbers cannot be more than four digits.")
break

if operator == "+":
sum = str(a + b)
elif operator == "-":
sum = str(a - b)

length = max(len(firstnum), len(secondnum)) + 2
topline = str(firstnum).rjust(length)
bottomline = operator + str(secondnum).rjust(length - 1)
line = ""
result = str(sum).rjust(length)

if i != problems[-1]:
firstline += topline + '    '
secondline += bottomline + '    '
dividingline += line + '    '
resultsum += result + '    '
else:
firstline += topline
secondline += bottomline
dividingline += line
resultsum += result

if cond:
blockofsums = firstline + "\n" + secondline + "\n" + dividingline + "\n" + resultsum
else:
blockofsums = firstline + "\n" + secondline + "\n" + dividingline

return blockofsums
``````

arithmetic_arranger([“231 + 800”, “34 + 6415”, “308 + 8”, “4 + 3”], True)

Nothing at all happens when you click run?

Can you click show hidden files:

Open .replit and confirm it says: run = “python main.py”?

print(“Error: Too many problems”)

Your function needs to `return` this message, not `print()` it. All the tests expect all output to be returned, it doesn’t read what gets printed.

Hi pkdvalis,

I’ve managed to get the code to work and fulfill all the requirements of the project now.

However… I still can’t get it run in Replit for some weird reason…
I can only see that it fulfills the requirements by having the code print those commands, rather than returning them.
This is what I see

Is there something wrong with the code here?

When I went into the hidden files as you suggested above, the .replit file didn’t say run = “python3 main.py” in line 1, but I’ve added it now. Regardless, it still doesn’t test the test_cases against my code.

Any help would be appreciated! After tweaking the code a bit more, I know I’ve fulfilled all the task requirements now. It’s just frustrating that I’m having difficulty uploading it successfully to Replit…

And this is what I see for the main.py file: