Reputation: 1
def arithmetic_arranger(problems, show_answers=False):
#error handling:problems greater than 5
if len(problems) > 5:
return 'Error: Too many problems.'
#splitting problems between operator and numbers
split_problems = ''
for problem in problems:
numberA, operator, numberB= problem.split()
#error handling of problems- only digits allowed
if not (numberA.isdigit() and numberB.isdigit()):
return 'Error: Numbers must only contain digits.'
#error handling of problems-no more than 4 digits
if len(numberA) > 4 or len(numberB) > 4:
return 'Error: Numbers cannot be more than four digits.'
# Error handling of problems- incorrect operator
if operator not in ('+', '-'):
return "Error: Operator must be '+' or '-'."
#define needed width
width = max(len(numberA), len(numberB)) + 2
#arranging the sums
split_problems += numberA.rjust(width) + '\n'
split_problems += operator + numberB.rjust(width - 1) + '\n'
split_problems += '-' * width + '\n'
return split_problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
For the code above, I'm not too sure how to show the solution in a horizontal line since my solution at the moment is one answer below the other instead of next to each other.
You don't need to show me the solution just please explain what I can do to have it in a horizontal line next to each other than below each other.
I'm getting:
xxx
+xx
___
xxx
-xx
___
xxx
+xx
___
xxx
+xx
___
instead of:
none
xxx (four spaces) another here (four spaces) another here (four spaces) another here
+xx
___
All indentations are accurate - I just couldn't do that here.
Upvotes: 0
Views: 122
Reputation: 1313
You can separately handle the 3 lines and add 4 blank spaces each time. At the end concat the 3 libes with '' separator, then display the string.
You can update your code this way:
def arithmetic_arranger(problems, show_answers=False):
#error handling:problems greater than 5
if len(problems) > 5:
return 'Error: Too many problems.'
#splitting problems between operator and numbers
split_problems1 = ''
split_problems2 = ''
split_problems3 = ''
for problem in problems:
numberA, operator, numberB= problem.split()
#error handling of problems- only digits allowed
if not (numberA.isdigit() and numberB.isdigit()):
return 'Error: Numbers must only contain digits.'
#error handling of problems-no more than 4 digits
if len(numberA) > 4 or len(numberB) > 4:
return 'Error: Numbers cannot be more than four digits.'
# Error handling of problems- incorrect operator
if operator not in ('+', '-'):
return "Error: Operator must be '+' or '-'."
#define needed width
width = max(len(numberA), len(numberB)) + 2
spacer = 4
#arranging the sums
split_problems1 += numberA.rjust(width) + ' '*spacer
split_problems2 += operator + numberB.rjust(width - 1) +' '*spacer
split_problems3 += '-' * width + ' '*spacer
split_problems1 += '\n'
split_problems2 += '\n'
split_problems3 += '\n'
split_problems = split_problems1 + split_problems2 + split_problems3
return split_problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
The result is:
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
Upvotes: 0
Reputation: 48
To do this, you would need to take all of your first numbers and put them in a list. Then, use a for loop and do something like this:
printstr = ""
for v, i in enumerate(numbers):
if not v == len(numbers) - 1:
printstr = printstr + f"{i}\t"
else:
printstr = printstr + f"{i}"
Then, you can print printstr and repeat the process for the rest of the numbers. (also, you can add a system that does the same thing for the lines that separate the equation and answer)
Upvotes: -1