[SOLVED] Arithmetic arranger, test results don't coincide with my output

Tell us what’s happening:
Describe your issue in detail here.
I’m only trying to debug for the first test at the moment, I can get the correct outputs on the console, but my code won’t pass the test.

Your code so far
def arithmetic_arranger(list, show=False):

initial_string = ''
for c, v in enumerate(list):
    initial_string += list[c] + ' '
real_list = initial_string.split()

dashes = 0
real_string = ""
dashes_list = []

for c in range(0, len(real_list), 3):
    if len(real_list[c]) > len(real_list[c + 2]) or len(real_list[c]) == len(real_list[c + 2]):
        dashes = len(real_list[c]) + 2
    elif len(real_list[c]) < len(real_list[c + 2]):
        dashes = len(real_list[c + 2]) + 2
    dashes_list.append(dashes)

    real_string += ' ' * (dashes - len(real_list[c])) + real_list[c] + ' ' * 4

real_string += '\n'

for c in range(1, len(real_list), 3):
    if len(real_list[c - 1]) > len(real_list[c + 1]) or len(real_list[c - 1]) == len(real_list[c + 1]):
        dashes = len(real_list[c - 1]) + 2
    elif len(real_list[c - 1]) < len(real_list[c + 1]):
        dashes = len(real_list[c + 1]) + 2

    real_string += real_list[c] + ' ' * (dashes - (len(real_list[c + 1]) + 1)) + real_list[c + 1] + ' ' * 4

real_string += '\n'

for c, v in enumerate(dashes_list):
    real_string += '-' * dashes_list[c] + ' ' * 4

# show
show_list = []
if show:
    real_string += '\n'
    for c in range(0, len(real_list), 3):
        if real_list[c + 1] == '+':
            a = int(real_list[c]) + int(real_list[c + 2])
            show_list.append(str(a))
        elif real_list[c + 1] == '-':
            a = int(real_list[c]) - int(real_list[c + 2])
            show_list.append(str(a))
    for c, v in enumerate(dashes_list):
        real_string += ' ' * (dashes_list[c] - len(show_list[c])) + show_list[c] + ' ' * 4
return real_string

Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

What does the test suite say? That will greatly help us help you.


Here is a screenshot of the results, I’ve managed to pass the other tests, if you find it relevant, here is the updated code:

def arithmetic_arranger(list, show=False):

# test_too_many_problems
if len(list) > 5:
    return "Error: Too many problems."

initial_string = ''
for c, v in enumerate(list):
    initial_string += list[c] + ' '
real_list = initial_string.split()

for c, v in enumerate(real_list):
    if len(real_list[c]) >= 5:
        return "Error: Numbers cannot be more than four digits."
    if not real_list[c].isnumeric() and real_list[c] not in '+-/*':
        return "Error: Numbers must only contain digits."
dashes = 0
real_string = ""
dashes_list = []

if '/' in real_list:
    return "Error: Operator must be '+' or '-'."

for c in range(0, len(real_list), 3):
    if len(real_list[c]) > len(real_list[c + 2]) or len(real_list[c]) == len(real_list[c + 2]):
        dashes = len(real_list[c]) + 2
    elif len(real_list[c]) < len(real_list[c + 2]):
        dashes = len(real_list[c + 2]) + 2
    dashes_list.append(dashes)

    real_string += ' ' * (dashes - len(real_list[c])) + real_list[c] + ' ' * 4

real_string += '\n'

for c in range(1, len(real_list), 3):
    if len(real_list[c - 1]) > len(real_list[c + 1]) or len(real_list[c - 1]) == len(real_list[c + 1]):
        dashes = len(real_list[c - 1]) + 2
    elif len(real_list[c - 1]) < len(real_list[c + 1]):
        dashes = len(real_list[c + 1]) + 2

    real_string += real_list[c] + ' ' * (dashes - (len(real_list[c + 1]) + 1)) + real_list[c + 1] + ' ' * 4

real_string += '\n'

for c, v in enumerate(dashes_list):
    real_string += '-' * dashes_list[c] + ' ' * 4

# show
show_list = []
if show:
    real_string += '\n'
    for c in range(0, len(real_list), 3):
        if real_list[c + 1] == '+':
            a = int(real_list[c]) + int(real_list[c + 2])
            show_list.append(str(a))
        elif real_list[c + 1] == '-':
            a = int(real_list[c]) - int(real_list[c + 2])
            show_list.append(str(a))
    for c, v in enumerate(dashes_list):
        real_string += ' ' * (dashes_list[c] - len(show_list[c])) + show_list[c] + ' ' * 4
return real_string

I would be much easier for me to read if you copy-paste the error message. I’m taking a look currently. It looks like you have extra spaces at the end of each line.

I don’t know how to copy from the console, sorry, I’ll take a look at the extra spaces

I managed to pass it by removing the spaces at the end, thanks!

def arithmetic_arranger(list, show=False):

# test_too_many_problems
if len(list) > 5:
    return "Error: Too many problems."

initial_string = ''
for c, v in enumerate(list):
    initial_string += list[c] + ' '
real_list = initial_string.split()

for c, v in enumerate(real_list):
    if len(real_list[c]) >= 5:
        return "Error: Numbers cannot be more than four digits."
    if not real_list[c].isnumeric() and real_list[c] not in '+-/*':
        return "Error: Numbers must only contain digits."
dashes = 0
real_string = ""
dashes_list = []

if '/' in real_list:
    return "Error: Operator must be '+' or '-'."
count = 0
for c in range(0, len(real_list), 3):
    count += 1
    if len(real_list[c]) > len(real_list[c + 2]) or len(real_list[c]) == len(real_list[c + 2]):
        dashes = len(real_list[c]) + 2
    elif len(real_list[c]) < len(real_list[c + 2]):
        dashes = len(real_list[c + 2]) + 2
    dashes_list.append(dashes)
    if count != len(list):
        real_string += ' ' * (dashes - len(real_list[c])) + real_list[c] + ' ' * 4
    else:
        real_string += ' ' * (dashes - len(real_list[c])) + real_list[c]

real_string += '\n'

count1 = 0
for c in range(1, len(real_list), 3):
    count1 += 1
    if len(real_list[c - 1]) > len(real_list[c + 1]) or len(real_list[c - 1]) == len(real_list[c + 1]):
        dashes = len(real_list[c - 1]) + 2
    elif len(real_list[c - 1]) < len(real_list[c + 1]):
        dashes = len(real_list[c + 1]) + 2
    if count1 != len(list):
        real_string += real_list[c] + ' ' * (dashes - (len(real_list[c + 1]) + 1)) + real_list[c + 1] + ' ' * 4
    else:
        real_string += real_list[c] + ' ' * (dashes - (len(real_list[c + 1]) + 1)) + real_list[c + 1]
real_string += '\n'

count2 = 0
for c, v in enumerate(dashes_list):
    count2 += 1
    if count2 != len(list):
        real_string += '-' * dashes_list[c] + ' ' * 4
    else:
        real_string += '-' * dashes_list[c]

# show
show_list = []
if show:
    count3 = 0
    real_string += '\n'
    for c in range(0, len(real_list), 3):
        if real_list[c + 1] == '+':
            a = int(real_list[c]) + int(real_list[c + 2])
            show_list.append(str(a))
        elif real_list[c + 1] == '-':
            a = int(real_list[c]) - int(real_list[c + 2])
            show_list.append(str(a))
    for c, v in enumerate(dashes_list):
        count3 += 1
        if count3 != len(list):
            real_string += ' ' * (dashes_list[c] - len(show_list[c])) + show_list[c] + ' ' * 4
        else:
            real_string += ' ' * (dashes_list[c] - len(show_list[c])) + show_list[c]
return real_string
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.