Arithmetic formatter - getting started

This is very rudimentary. Initially I thought to use regex to save everything before an operand into one list, save operands to another list, and everything after operands to another list, and then loop through lists and pipe things out in a certain format, but using split() on lists and strings might be easier. But split() isn’t acting as I expect it to. I’m trying to get back into Python after an absence and I’m very rusty. I will keep searching through other questions posted as well.

I am passing in this argument list outside of the function:
print(arithmetic([‘1+11’, ‘33+2’, ‘34+33’, ‘99+31’]))

I was expecting this code to give me the argument list split into its composite strings, and then those strings split into individual characters, but I get an error that split is not defined:

# split argument list into a list of lists
for problem in problems:
    lines = problem.split()
    print(lines)
    for line in lines:
        print(split(line))

output:
% python3 arith.py
[‘1+11’]
Traceback (most recent call last):
File “arith.py”, line 19, in
print(arithmetic([‘1+11’, ‘33+2’, ‘34+33’, ‘99+31’]))
File “arith.py”, line 16, in arithmetic
print(split(line))
NameError: name ‘split’ is not defined

This code prints the original argument list as a list of lists, and then prints out a list of strings?

# split argument list into a list of lists
for problem in problems:
    lines = problem.split()
    print(lines)
    for line in lines:
        print(split(line))

output:
% python3 arith.py
[‘1+11’]
1+11
[‘33+2’]
33+2
[‘34+33’]
34+33
[‘99+31’]
99+31
None

And un-nesting the second for loop prints the original argument list, and then the split list ends up only holding the last string from the list?

# split argument list into a list of lists
for problem in problems:
    lines = problem.split()
    print(lines)
for line in lines:
    print(line)

output:
% python3 arith.py
[‘1+11’]
[‘33+2’]
[‘34+33’]
[‘99+31’]
99+31
None

To confuse me even further, changing the first print statement to a return (I was thinking maybe this would return the split list and allow a second use of split) ends up printing out only the first list in the list of lists resulting from the first use of split:

# split argument list into a list of lists
for problem in problems:
    lines = problem.split()
    return(lines)
for line in lines:
    print(line)

output:
% python3 arith.py
[‘1+11’]

I clearly have a lot to review, but I’m confused as to why I’m so lost. Any help is very much appreciated.

Hi…

Let’s look at the split function first… It takes a string and splits it into a list of strings using whichever separator you specify - by default, if you omit the separator, it separates by white space.

So you could split a string by a line break, a white space, a comma, etc, then handle the terms separately…

The syntax is 'some string'.split(separator)

Remember that return, unlike a print statement, tells the function that we got the value we were looking for and exits the function call…

Hope that helped at least a little… Do feel free to reply with feedback and further questions - I’m relatively new to this forum too but there is a really helpful crowd here! I’m sure you’ll get more replies soon too…

Its probably a little frustrating returning to code after a while - I totally got that rusty feeling too :slight_smile: Here’s a quote/maxim from the freecodecamp home page to make you feel less alone in feeling so:

“Any code of your own that you haven’t looked at for six or more months might as well have been written by someone else”
Eagleson’s Law [of Programming]”

1 Like

My argument list that I passed in was lacking any delimiter because I didn’t have spaces between the operator and operands.

Yep!

Here are a few little things I would do to make the process easier…

From any one of the unit tests in the test module script, I’d copy one of the inputs passed into the function we are writing and work with that from the start. That way, once we write the algorithm, we won’t have to reformat it.

Also, I’d plan out all the steps/functions I would need before writing code (scratch notes or visualising it) keeping in mind what variable values I expect at each step - and then proceed to write in chunks through the program, printing at each stage to make sure that I catch the bits that don’t work as I planned out…

Happy coding! This one can get a little frustrating with the spaces but that’s the fun part too!

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