vendredi 21 mai 2021

Arithmetic Formatter Python Ending

so I’ve been following the Python freeCodeCamp curriculum and I am now doing my first mini project.

What we are meant to do is the following: "Create a function that receives a list of strings that are arithmetic problems and returns the problems arranged vertically and side-by-side."

So I've been writing my code and everything works fine but I've reached the end of this function and I cannot seem to find a way to align my code (to the right). I've tried using rjust() method but I just cannot seem to figure it out. I've been stuck on this one for a while now.

I have attached my code so you can take a look (it is possible that I might have over complicated things which I would not be surprised but hey, everyone starts somewhere and with experience, I'll certainly right better code)

Thanks a lot in advance for your help :)!

problems = ["32 + 6984", "3801 - 2", "45 + 43", "123 + 49"]

first_operand = list()
operator = list()
second_operand = list()
operation = list()
index = 0
dashes = ""
upper_part = ""
lower_part = ""
dashelines = ""
final_part = ""


# Get the length of the list, if it is higher than 5, print an error 
if len(problems) > 5:
    print(" Error: Too many problems")

# Divide the list into sub lists
for increment in problems:
    new_item = increment.split()

    # Assign first, second operand and operator
    first_operand.append(new_item[0])
    operator.append(new_item[1])
    second_operand.append(new_item[2])

# Checking if the operands are only digits (must be separated into 
# two different for loops if we do not want to see the error written 4 times)
for go_over in first_operand:
    if not go_over.isnumeric():
        print("Error: Numbers must only contain digits")
for go_over in second_operand:
    if not go_over.isnumeric():
        print("Error: Numbers must only contain digits")

# Checking if numbers are not above 4 digits (must be separated into 
# two different for loops if we do not want to see the error written 4 times)
for go_over in first_operand:
    if len(go_over) > 4:
        print("Error: Numbers cannot be more than four digits")
for go_over in second_operand:
    if len(go_over) > 4:
        print("Error: Numbers cannot be more than four digits")
        
# Checking if the operator is a '+' or a '-'
for go_over in operator:
    index += 1
    if go_over == "*" or go_over == "/":
        print("Error: Operator must be '+' or '-'")
    # Doing the calculation for '+' and '-'
    elif go_over == "+":
        operation.append(str(int(first_operand[index-1]) + int(second_operand[index-1])))
    elif go_over == "-":
        operation.append(str(int(first_operand[index-1]) - int(second_operand[index-1])))


# Getting the maximum length of both operands in order to have the right amount of dashes
max_length = max(len(first_operand),len(second_operand)) + 2

# Getting the maximum length of dashes under each operation
for num_dashes in range(max_length):
    dashes += "-"

for go_over in range(len(first_operand)):
    if go_over == len(first_operand):
        upper_part += first_operand[go_over]
        dashelines += dashes
    else:
        upper_part += first_operand[go_over] + "    "
        dashelines += dashes + "    "

for go_over in range(len(second_operand)):
    if go_over == len(second_operand):
        lower_part += second_operand[go_over]
    else:
        lower_part += second_operand[go_over] + "    "

for go_over in range(len(operation)):
    if go_over == len(operation):
        final_part += operation[go_over]
    else:
        final_part += operation[go_over] + "    "``

Aucun commentaire:

Enregistrer un commentaire