Home>

Please write your question in detail here.
This is a Python question.
I want to display the answer of an expression like 2 1+ written in Reverse Polish Notation

(1) At the time of calculation, two numbers (stack [0] and stack [1]) are taken out from the stack and the calculation is performed.
(2) When the calculation is completed, delete the two used numbers from the stack, and then put the answer of (1) on the stack.
③ As a result, the last answer is at the top of the stack
You can use input, print, while, if, len, str
The following error message occurred while implementing the above function. Please tell me how to make it work properly.

stack = shiki [n] + stack
TypeError: can only concatenate str (not "list") to str
Corresponding source code
Source code
shiki = input ()

stack = []
a = len (shiki)
n = 0
while n<= a:
 if shiki [n] == "+":
    register2 = int (stack [0])
    register1 = int (stack [1])
    kotae = register1 + register2
    stack = stack [2:]
    stack = str (kotae) + stack

 else: else:
    stack = shiki [n] + stack
kotae = stack [0]
print (kotae)
What I tried

Please describe here what you have tried for the problem.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    Well, usually to insert an element anywhere in the listinsertIs used. If you want to add it to the beginning (position 0)stack.insert (0, shiki [n])is not it.

    However, this timeinsertIt's like a binding that can't be used, so use slices to get the same result.stack [0: 0] = shiki [n]is. The process of addition is the same.

    Besides this,

    To get the "answer of an expression like 2 1 +", you need to skip the blanks (no need for an input like "2 1+")

    nThe process to increase is not written

    The boundary condition for repeating while is incorrect

    Please solve the problem yourself.

  • Answer # 2

    It looks good, but there are some problems.

    ■ The part that caused the error.
    The process of adding data to the top of the stack must be done between lists.
    【Part 1】

      stack = str (kotae) + stack
      stack = [str (kotae)] + stack


    [Part 2]

    stack = shiki [n] + stack
    stack = [shiki [n]] + stack

    ■ Input must be listed
    I don't know how to enter it, but if you separate it with spaces like "1 3 4 +", divide it with spaces to make a list.

    shiki = input (). split ((''))

    ■ The n increment in while is missing.
    Add the following line to the end of the while loop

     n + = 1

    ■ The loop condition is incorrect.

    Since n starts from 0, the judgment of while isn<= anotn is.

    I think this will work.
    ---
    bonus
    It's hard to get values ​​in and out of the top of a python list.
    On the other hand, if you are from the back, you can use pop and append to make it stack-like.
    If you write only the while part, it looks like this.

    python
    while n if shiki [n] == "+":
    register2 = int (stack.pop ())
    register1 = int (stack.pop ())
    kotae = register1 + register2

    stack.append (str (kotae))

    else: else:
    stack.append (shiki [n])

    n + = 1
    `` ```
    * Normally, push is paired with pop in stack, but append is used in python.