# Trying to learn and would appreciate some help

Tell us what’s happening:
Hi,
I have tried to get into programming and learn python. The arithmatic arranger problem in freecodecamp. The problem is whenever I run my code varaible in list always starts in a new line instead of being in the same line. Any help is appreciated. Thank you in advance

Your code so far

``````def arithmetic_arranger(problems):
if len(problems) > 5:
return ("Error: Too many problems.")
for value in problems:
setnum = value.split(" ")
if setnum[1] !="+" and setnum[1] !="-":
return("Error: Operator must be '+' or '-'.")
try: setnum[0] == int(setnum[0])
except : return("Error: Numbers must only contain digits.")
try: setnum[2] == int(setnum[2])
except : return("Error: Numbers must only contain digits.")
if len(setnum[0]) > 4 or len(setnum[2]) > 4:
return("Error: Numbers cannot be more than four digits.")
if setnum[1] == "+" :
answer = int(setnum[0]) + int(setnum[2])
else: answer = int(setnum[0]) - int(setnum[2])
leng = max(len(setnum[0]), len(setnum[2]))
top= str(setnum[0]).rjust(leng + 2)
operand = setnum[1]
mid = str(setnum[2]).rjust(leng)
string = ''.rjust(leng+2,'-')
low = str(answer).rjust(leng +2)
print (top + '\n' + operand +' '+ mid + '\n'+ string +'\n' + low)
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36`

Challenge: Arithmetic Formatter

Link to the challenge:

Given indentation is so import to Python, could you format your code so we are able to see it?
You can do this by placing it between two rows of three backticks:

```
```

1 Like

Thank you for your reply. Below is my code

``````def arithmetic_arranger(problems):
if len(problems) > 5:
return ("Error: Too many problems.")
for value in problems:
setnum = value.split(" ")
if setnum[1] !="+" and setnum[1] !="-":
return("Error: Operator must be '+' or '-'.")
try: setnum[0] == int(setnum[0])
except : return("Error: Numbers must only contain digits.")
try: setnum[2] == int(setnum[2])
except : return("Error: Numbers must only contain digits.")
if len(setnum[0]) > 4 or len(setnum[2]) > 4:
return("Error: Numbers cannot be more than four digits.")
if setnum[1] == "+" :
answer = int(setnum[0]) + int(setnum[2])
else: answer = int(setnum[0]) - int(setnum[2])
leng = max(len(setnum[0]), len(setnum[2]))
top= str(setnum[0]).rjust(leng + 2)
operand = setnum[1]
mid = str(setnum[2]).rjust(leng)
string = ''.rjust(leng+2,'-')
low = str(answer).rjust(leng +2)
print (top + '\n' + operand +' '+ mid + '\n'+ string +'\n' + low)
``````

Given how they want you to align text, I think you’re gonna have to take a different approach with things. Currently you evaluate the problems one at a time and then print them, but because of what they need you to do, you might have to look at evaluating them all at one.

One idea I have would be to create a class for each problem, that stores the top middle and bottom. Then you could compile all of these into a list, and at the end parse them together.

Sounds fun, I think I’ll have to spend some time on the drawing board

Another idea would be to just create a multidimensional list and then iterate through that.

1 Like

Ofcourse it does. How is Python supposed to know you want to put the output of the print on an arbitrary line in the console?

Now before you bother trying to figure out that this is impossible (the console doesn’t allow edits to the history): You are supposed to “return” the result. Meaning you have to put the entire result of the code into a single string.
Now you gotta think about how you can make sure the string looks like the lines asked for in the task

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

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