Tell us what’s happening:
So I am working on Scientific Computing with Python Projects - Arithmetic Formatter
and here is the criteria
Create a function that receives a list of strings that are arithmetic problems and returns the problems arranged vertically and side-by-side. The function should optionally take a second argument. When the second argument is set to True
, the answers should be displayed.
Constraints:
-
Limit to 5 problems max for each input or else it will print
“Error: too many problems” -
Addition and Subtraction only
-
Each operand should only be digits
-
Operand as at most 4 digits
So I am using pycharms and according to the criteria it satisfies all the constraints and this is my code:
Your code so far
def arithmetic_arranger(alist, *args):
alen = len(alist)
if (alen > 5):
return "Error: Too many problems."
firstline = ""
secondline = ""
tline = ""
lastline = ""
for x in alist:
checker = x.count("*")
checker2 = x.count("/")
if checker | checker2 > 0:
return "Error: Operator msut be '+' or '-'."
txt = x.split(" ")
plen = max(len(txt[0]), len(txt[2]))
if plen > 4:
return "Error: Numbers cannot be more than four digits."
if txt[0].isdigit() & txt[2].isdigit():
if (plen == len(txt[0])):
fline = " " + txt[0] + " "
firstline = firstline + fline
diff = len(txt[0]) - len(txt[2]) + 1
dspace = " " * diff
sline = txt[1] + dspace + txt[2] + " "
secondline = secondline + sline
tl = (plen + 2) * "_" + " "
tline = tline + tl
if txt[1] == "+":
a1 = int(txt[0])
a2 = int(txt[2])
addlines = a1 + a2
ss2 = str(addlines)
a3 = len(tl) - 3 - len(ss2)
ss3 = " " * a3
ss2 = ss3 + ss2 + " "
lastline = lastline + ss2
else:
s1 = int(txt[0])
s2 = int(txt[2])
sublines = s1 - s2
ss2 = str(sublines)
s3 = len(tl) - 3 - len(ss2)
ss3 = s3 * " "
ss2 = ss3 + ss2 + " "
lastline = lastline + ss2
else:
diff = len(txt[2]) - len(txt[0]) + 2
dspace = " " * diff
fline = dspace + txt[0] + " "
firstline = firstline + fline
sline = txt[1] + " " + txt[2] + " "
secondline = secondline + sline
tl = (plen + 2) * "_" + " "
tline = tline + tl
if txt[1] == "+":
a1 = int(txt[0])
a2 = int(txt[2])
addlines = a1 + a2
ss2 = str(addlines)
a3 = len(tl) - 3 - len(ss2)
ss3 = " " * a3
ss2 = ss3 + ss2 + " "
lastline = lastline + ss2
else:
s1 = int(txt[0])
s2 = int(txt[2])
sublines = s1 - s2
ss2 = str(sublines)
s3 = len(tl) - 3 - len(ss2)
ss3 = s3 * " "
ss2 = ss3 + ss2 + " "
lastline = lastline + ss2
else:
return "Error: Numbers must only contain digits"
if args:
arr = [firstline, secondline, tline, lastline]
for x in arr:
print(x)
else:
arr = [firstline, secondline, tline]
for x in arr:
print(x)
Your browser information:
On my pycharms ide it runs perfectly fine with no errors but when i copy and paste it on the webpage it keeps running into errors
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
.
Challenge: Arithmetic Formatter
Link to the challenge: