ABSTRACT
This project presents a Python-based solution to format simple arithmetic problems vertically and side by side. The function processes a list of addition and subtraction expressions and arranges them in a structured, readable format similar to handwritten calculations. The primary objective is to enhance understanding of string manipulation, error handling mostly by using conditional statements, and formatting in Python programming. The function also provides the option to display the results of each calculation. This task is appropriate for programming beginners and supports foundational skills in structured problem-solving.
CONTENTS
ABSTRACT
CONTENTS
1 INTRODUCTION
2 PYTHON CODE AND PERSONAL SOLUTION
3 OUTPUT
4 CONCLUSION AND DISCUSSION
1 INTRODUCTION
2 PROJECT REQUIREMENT AND PYTHON CODE WITH PERSONAL SOLUTION
The python function developed is named as follows:
arithmetic_arranger(problems, show_answers=False)
|
def arithmetic_arranger(problems, show_answers=False):# First constraintif len(problems) > 5:return 'Error: Too many problems.'# BREAKING DOWN THE FORMATfirst_operands = [first_operand.split()[0] for first_operand in problems] # List of first operandsoperators = [operator.split()[1] for operator in problems] # List of operatorssecond_operands = [second_operand.split()[2] for second_operand in problems] # List of second operands# Constraints for inputfor i in range(len(problems)):if operators[i] not in ('+', '-'):return "Error: Operator must be '+' or '-'."if not first_operands[i].isdigit() or not second_operands[i].isdigit():return 'Error: Numbers must only contain digits.'if len(first_operands[i]) > 4 or len(second_operands[i]) > 4 or len(first_operands[i]) < 1 or len(second_operands[i]) < 1:return 'Error: Numbers cannot be more than four digits.'# BREAKING DOWN ELEMENTS OF EACH PROBLEManswers = [(int(first_operands[i]) + int(second_operands[i])) if operators[i] == '+' else (int(first_operands[i]) - int(second_operands[i])) for i in range(len(problems))] # List of answerswidth = [max(len(first_operands[i]), len(second_operands[i])) for i in range(len(problems))] # Width of each problem block when formattingdashes = ['-'*(width[i] + 2) for i in range(len(problems))] # List of dashes used for formatting# MODIFYING LIST1 AND LIST2# Space multipliers for each line presentedspace_l1 = [] # List of space multipliers for list 1space_l2 = [] # List of space multipliers for line 2space_al = [] # List of space multipliers for answer linefor i in range(len(problems)):space_l1.append(len(dashes[i]) - len(first_operands[i]))space_l2.append(len(dashes[i]) - len(second_operands[i]) - 1)space_al.append(len(dashes[i]) - len(str(answers[i])))#LINES OF OPERATIONSline_1 = [(' '*space_l1[i]) + first_operands[i] for i in range(len(problems))] # First operandsline_2 = [operators[i] + (" " * space_l2[i]) + second_operands[i] for i in range(len(problems))] # Operators and second operandsanswer_line = [(" " * space_al[i]) + str(answers[i]) for i in range(len(problems))]# FORMATTING THE PROBLEMS# Format without answersarranged_format = (" ".join(line_1) + "\n" +" ".join(line_2) + "\n" +" ".join(dashes))# Format with answers includedif show_answers:return arranged_format + "\n" + " ".join(answer_line)else:return arranged_format# TESTING CALLS#print(arithmetic_arranger(["3801 - 2", "123 + 49"]))#print(arithmetic_arranger(["1 + 2", "1 - 9380"]))#print(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]))#print(arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]))#print(arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"]))#print(arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"]))#print(arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"]))#print(arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"]))#print(arithmetic_arranger(["3 + 855", "988 + 40"], True))#print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True))
3 OUTPUT
4 CONCLUSION AND DISCUSSION
One of the key challenges was ensuring all formatting was consistent across varying lengths of operands. Additionally, including detailed error messages enhances the robustness and user-friendliness of the function.
Overall, this task is an effective exercise for beginner programmers and can serve as a stepping stone toward more complex text-based applications in Python.
FIGURE 3. The manual ideation at first stage before coding. |
test test
ReplyDelete