Hi,
On my 3rd week of learning python now, enjoying it, apart from the inevitable head aches!
I’ve been writing my code in pycharm, rather than replit, as I know how to debug there. I’ve hardcoded calls to arithmetic_arranger into it, with the ‘problems’ from test_module.py formatted in a single pair of braces. Works great.
But test_module.py has them doubled, to contain the optional argument. Silly me, should have checked there first!
If I hardcode in double braces I get the understandable error ‘list’ object has no attribute ‘split’. Fair enough…
But then my code failed on the arguments at lines 41 and 51 of the test_module; it’s never done that in pycharm. ( ‘Operator must be ‘+’ or ‘-’.’ and ‘Error: Numbers must only contain digits’)
This is so different to it’s behaviour in pycharm that its got me stumped. And theres nothing helpful in the traceback (will post a screengrab of that if queried.)
So I was going to run test_module.py in pycharm, so I can step through its operation. But theres a lot in there thats new to me, calls to libraries/modules that are unfamiliar, and I’m wondering if that’s even the best strategy in this situation.
def arithmetic_arranger(problems, prnt_ans=None):
top_l =''
btm_l =''
dash_l =''
ans_l =''
print(problems)
for s in problems:
if len(problems) > 5: return'Error: Too many problems.'
lst = s.split()
if not lst[0].isdigit() or not lst[2].isdigit(): return'Error: Numbers must only contain digits."'
if len(lst[0]) > 4 or len(lst[2]) > 4: return'Error: Numbers cannot be more than four digits.'
if lst[1] != '+' and lst[1] != "-": return"Error: Operator must be ' + ' or ' - '."
calc = {'+': lambda x, y: x + y, '-': lambda x, y: x - y}
ans = str((calc[lst[1]](int(lst[0]), int(lst[2]))))
n = max(len(lst[0]) + 2, len(lst[2]) + 2, len(ans))
top_l = top_l + " " * (n - len(lst[0])) + ((lst[0]) + ' ')
btm_l = btm_l + ((lst[1]).ljust(n -len(lst[2])) + (lst[2]) + ' ')
dash_l = dash_l + "-" * n + ' '
ans_l = ans_l + ans.rjust(n) + ' '
arranged_problems = (top_l.rstrip() + '\n' + btm_l.rstrip() + '\n' + dash_l.rstrip())
if prnt_ans: arranged_problems = (top_l.rstrip() + '\n' + btm_l.rstrip() + '\n' + dash_l.rstrip() + '\n' + ans_l.rstrip())
return arranged_problems
problems = [['44 + 815', '909 - 2', '45 + 43', '123 + 49', '888 + 40', '653 + 87']]
print(arithmetic_arranger(problems))
I ran it without the last 2 lines on replit. Its at https://replit.com/@moorea21/boilerplate-arithmetic-formatter-1#arithmetic_arranger.py but might have changed by the time anyone reads it.
Any debugging advice would be welcome here.