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.