Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
I am new to programing and am doing this course. My problem is with the first project. It doesnt look like what I was thought about it in the course prior is enough to pass it. My code is below(I made it only in one python file for now). I can make the Arithmedic Formatter but without it being four spaces between each problem.

Your code so far
problems = [‘32 + 8’,‘33 - 23’,‘485 + 2’, ‘154 + 145’]

for problem in problems:

    chunks = problem.split(" ")
    max_len = len(max(chunks,key=len))
    line_len = max_len + 2
    line = '-' * line_len

    current_len = len(chunks[0])
    first_num_len = line_len - current_len
    first_num = (" " * first_num_len) + chunks[0]

    current_len_2 = len(chunks[2])
    second_num_len = (line_len - current_len_2) - 1
    operation = chunks[1]
    second_num = (operation + " " * second_num_len) + chunks[2]

    arranged_problems = (first_num + "\n" + second_num + "\n" + line)

    result = None
    first_number_int = int(first_num)
    second_number_int = int(chunks[2])

    if operation == "+":
        result = first_number_int + second_number_int
    elif operation == "-":
        result = first_number_int - second_number_int

    current_len_3 = len(str(result))
    result_len = line_len - current_len_3
    result_aligned = (" " * result_len) + str(result)

    arranged_problems = (first_num + "\n" + second_num + "\n" + line + "\n" + result_aligned)

    print(arranged_problems + "\n")

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

Welcome to the course :slight_smile:

Yeah, the projects are meant to expand on the coursework, so usually you have to do a bit of research and brainstorming to solve them. But I believe it can be solved just using or expanding on what was shown in the course.

Anyway, I noticed you said you are just working on it in a single file… did you first download the “starter code” that was provided? The starter code has all the testing files and things used to test your software. Are you working on this on your local system (like in VSCode) or in Replit?

You might already know this, but the project asked you to create a function that returns the formatted string… your code so far is just a script that prints the desired output… just wanted to point that out if you didn’t know the difference, but you might just be doing it this way for testing and development.

Regarding the code… it runs and prints the output, but yeah, one right after another instead of in a row. The formatting of the problems is the primary issue you’re expected to solve in this challenge. That’s what makes it more difficult. I don’t see that you asked any question, so what are you having problems with… not sure how you should go about it?

If you’re wondering if there is some function you’re not familiar with that changes the output from column to row… sadly no… your task is to figure out how you can print them in rows using just what you know about strings, and theres many ways it can be done.

Think about it, and let us know if you have any questions or want to bounce some possible ideas… I thought about listing a few possible methods, but didn’t want to influence your solution. Its fun seeing how many different ways people come up with to solve these, although its even more surprising how often everyones completely independed solution ends up looking almost exactly like anothers.

Thanks for your answer, I managed to figure it out after all. I can post the code here if you are interested.

1 Like

Nice. Maintaining four separate lines is the most common solution I’ve seen for this problem. Can also split the problem peices into separate arrays and then build the string going through those.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.