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)
Your browser information:
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: