0

I am trying to create a calculator which will solve simple equations given in reverse polish notation. I have created a function for the actual calculation functionality, calculation(), as well as a function to take the input, inputs(). To get the inputs into calculation(), I tried to make inputs() an argument for that function. The issue is I can't seem to access the inputs list inside calculation() in order to perform the calculations. I'm not sure what I'm doing wrong/if this is even allowed as I'm fairly new to python.

# sorting input into integers, operators and eventually invalids
def inputs():
    operators = {'+', '-', '*', '/', '%', '='}
    inputs = []
    print("inputs: ", inputs)
    while True:
        blank = input()
        if blank not in operators:
            inputs.append(int(blank))
        else:
            inputs.append(blank)
        print("inputs: ", inputs)
        continue

#attempting to take inputs and use them in the calculations
def calculation(inputs):
    inputs = inputs()
    stack = []

    for a in inputs:
        if type(a) is int:
            stack.append(a)
            print("stack.append(a)", stack)
            continue

        op2, op1 = stack.pop(), stack.pop()

        if a == '+':    #code for addition
            result = op1 + op2
            stack.append(result)
#code for other operators is like above
up91
  • 1
  • 2
    your `inputs()` function is not returning anything. – drum Oct 31 '21 at 14:53
  • But yes, the _immediate_ problem is that you aren't calling `return inputs` inside `inputs()`. – Charles Duffy Oct 31 '21 at 14:59
  • (and do note that Stack Overflow calls for narrow, specific questions; "please fix all the bugs in my program" is too broad; whereas "why is inputs == None` after calling `inputs = inputs()` with this inputs() function?" is pretty much right from a scoping perspective) – Charles Duffy Oct 31 '21 at 15:10
  • @CharlesDuffy, thank you for your comments. The issue is when I call 'return inputs' inside 'inputs()' it doesn't continue to append the inputs to the list. I tried to put the code within 'inputs()' in 'calculation()' but again, I can't access the inputs list for the 'for' loop – up91 Oct 31 '21 at 16:27
  • Put `return inputs` at the _end_ of the function, when it's expected to exit. Otherwise, by default, the end of the function acts like it has `return None`. – Charles Duffy Oct 31 '21 at 17:52

0 Answers0