Tell us what’s happening:
I’m having trouble getting the formatted arithmetic problems to print horizontally instead of vertically. I have tried several different things (including print(variable, end=’ ')) to get the problems to print horizontally, but nothing seems to be working.
Any idea or suggestion as to how I can edit the below code to make the problems print horizontally?
I currently have it set to print neatly vertically, but know the new lines will need to be removed.
Your code so far
def arithmetic_arranger(problems, show_solution=False):
“”"
Will take a list of arithmetic problems and arrange
them as they would appear on a grade school assignment
sheet. The solution will also appear if show_solution is set to True.
Only addition and subtraction are valid operators, no number can be greater than 4 digits long, and there can be no more than 5 arithmetic problems entered.
Example: '32 + 8'
Output: 32
+ 8
----
"""
# Can't have more than 5 problems input
if len(problems) > 5:
return "Error: Too many problems."
# Create lists to store all of our values
top_nums, bottom_nums, operators = [], [], []
results, dashes, arranged, arranged_solution = [], [], [], []
# Loop through the input problems and sort each piece of information into the proper empty list created above.
for problem in problems:
entry = problem.split() # This should create a list of [num1, operator, num2]
# Check both entry[0] and entry[2] are numeric and store there values if so.
if entry[0].isnumeric() and entry[2].isnumeric():
top_nums.append(entry[0])
bottom_nums.append(entry[2])
else:
return "Error: Numbers must only contain digits."
# Check the operator is valid.
if entry[1] in ['+', '-']:
operators.append(entry[1])
else:
return "Error: Operator must be '+' or '-'."
# Calculate the result of the arithmetic problem.
if entry[1] == '+':
results.append(int(entry[0]) + int(entry[2]))
elif entry[1] == '-':
results.append(int(entry[0]) - int(entry[2]))
# Check if either number is > 4 digits and get the number of dashes.
if len(entry[0]) > 4 or len(entry[2]) > 4:
return "Error: Numbers cannot be more than four digits."
elif len(entry[0]) >= len(entry[2]):
dashes.append('-' * (len(entry[0]) + 2))
else:
dashes.append('-' * (len(entry[2]) + 2))
# Create the template to format of the arithmetic problems appropriately.
for i in range(len(problems)):
space = ' ' * ((len(dashes[i]) - 1 ) - len(bottom_nums[i]))
formatted_solution = f"{top_nums[i].rjust(len(dashes[i]))}\n{operators[i]}{space}{bottom_nums[i].rjust(len(bottom_nums[i]))}\n{dashes[i]}\n{str(results[i]).rjust(len(dashes[i]))}"
formatted = f"{top_nums[i].rjust(len(dashes[i]))}\n{operators[i]}{space}{bottom_nums[i].rjust(len(bottom_nums[i]))}\n{dashes[i]}"
arranged.append(formatted)
arranged_solution.append(formatted_solution)
# Display the correct arithmetic format based on if show_solution is displayed as True or not.
if show_solution:
return '\n'.join(arranged_solution)
else:
return '\n'.join(arranged)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36
.
Challenge: Arithmetic Formatter
Link to the challenge: