Tell us what’s happening:
The output on replit’s console has F letters before every Error print, one before one of the operations, and another one just before the output end. I have no idea why, since my code has no F on it. Because of that I am still getting all the failures.
I also get a “None” on the output, but I don’t know if that’s normal or not.
If someone could shine a light on this, I would be very grateful, thank you.
Your code so far
import re
def arithmetic_arranger(problems, sums=False):
#defining variables
n1 = ""
n2 = ""
op = ""
sum = ""
top_list = []
middle_list = []
slashes = []
sums = []
string = ""
#first obligatory error checking
if len(problems) > 5 :
print('Error: Too many problems.')
else :
#splitting the problem for easier manipulation
for problem in problems:
n1 = problem.split()[0]
n2 = problem.split()[2]
op = problem.split()[1]
#obligatory error checking
if op == '/' or op == '*':
print ("Error: Operator must be '+' or '-'.")
elif re.search('\D', n1) or re.search('\D', n2) :
print('Error: Numbers must only contain digits.')
elif len(n1) >= 5 or len(n2) >= 5 :
print('Error: Numbers cannot be more than four digits.')
else :
#doing the operation for sum=True statements
if op == "+" :
sum = str(int(n1) + int(n2))
elif op == "-" :
sum = str(int(n1) - int(n2))
#figuring out the numbers of slashes and spaces needed.
spaces_n1 = ""
spaces_n2 = ""
if len(n1) >= len(n2) :
slash = '-' * (len(n1) + 2)
spaces_n1 = ' ' * (len(slash) - len(n1))
spaces_n2 = ' ' * ((len(slash) - len(n2)) - 2)
slashes.extend(slash + " ")
elif len(n2) > len(n1) :
slash = '-' * (len(n2) + 2)
spaces_n1 = ' ' * (len(slash) - len(n1))
spaces_n2 = ' ' * ((len(slash) - len(n2)) - 2)
slashes.extend(slash + " ")
spaces_sum = ' ' * (len(slash) - len(sum))
#appending the parts needed to the lists for printing
top_list.extend([spaces_n1, n1, " "])
middle_list.extend([op, " ", spaces_n2, n2, " "])
sums.extend([spaces_sum, sum + " "])
#turning the lists into strings to concatenate in the print statement.
top_string = ''.join(top_list)
top_string = top_string.rstrip()
middle_string = ''.join(middle_list)
middle_string = middle_string.rstrip()
slashes_string = ''.join(slashes)
slashes_string = slashes_string.rstrip()
sums_string = ''.join(sums)
sums_string = sums_string.rstrip()
#check if sums=True for the appropriate print statement
if sums :
string = (top_string + "\n" + middle_string + "\n" + slashes_string + "\n" + sums_string + "\n")
print(string)
else :
string = (top_string + "\n" + middle_string + "\n" + slashes_string + "\n")
print(string)
Output
32
+ 698
-----
730
32 3801
+ 698 - 2
----- ------
730 3799
32 3801 45
+ 698 - 2 + 43
----- ------ ----
730 3799 88
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
730 3799 88 172
None
3
+ 855
-----
858
3 3801
+ 855 - 2
----- ------
858 3799
3 3801 45
+ 855 - 2 + 43
----- ------ ----
858 3799 88
3 3801 45 123
+ 855 - 2 + 43 + 49
----- ------ ---- -----
858 3799 88 172
FError: Operator must be '+' or '-'.
3801
- 2
------
3799
3801 45
- 2 + 43
------ ----
3799 88
3801 45 123
- 2 + 43 + 49
------ ---- -----
3799 88 172
FError: Numbers must only contain digits.
3801
- 2
------
3799
3801 45
- 2 + 43
------ ----
3799 88
3801 45 123
- 2 + 43 + 49
------ ---- -----
3799 88 172
F 32
- 698
-----
-666
32 1
- 698 - 3801
----- ------
-666 -3800
32 1 45
- 698 - 3801 + 43
----- ------ ----
-666 -3800 88
32 1 45 123
- 698 - 3801 + 43 + 49
----- ------ ---- -----
-666 -3800 88 172
FError: Numbers cannot be more than four digits.
3801
- 2
------
3799
3801 45
- 2 + 43
------ ----
3799 88
3801 45 123
- 2 + 43 + 49
------ ---- -----
3799 88 172
FError: Too many problems.
F
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36
Challenge: Arithmetic Formatter
Link to the challenge: