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  and stack ) 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.
Corresponding source code
stack = shiki [n] + stack TypeError: can only concatenate str (not "list") to str
What I tried
Source code shiki = input () stack =  a = len (shiki) n = 0 while n<= a: if shiki [n] == "+": register2 = int (stack ) register1 = int (stack ) kotae = register1 + register2 stack = stack [2:] stack = str (kotae) + stack else: else: stack = shiki [n] + stack kotae = stack  print (kotae)
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 list
insertIs used. If you want to add it to the beginning (position 0)
stack.insert (0, shiki [n])is not it.
However, this time
insertIt'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.
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.
stack = str (kotae) + stack
stack = [str (kotae)] + stack
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 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.
while n if shiki [n] == "+":
register2 = int (stack.pop ())
register1 = int (stack.pop ())
kotae = register1 + register2
stack.append (str (kotae))
stack.append (shiki [n])
n + = 1
* Normally, push is paired with pop in stack, but append is used in python.
- python 3x - about array calculation in python
- python - i want to speed up the calculation of for statements with numpy
- python polygon area calculation can't assign to operator
- i want to automatically generate a calculation problem in python
- python - saving the calculation result of the correlation coefficient
- the calculation result of python print (1/2) becomes 0
- error when performing numerical calculation with python
- python - about the reason why the notation of the sample of the decision tree visualized by graphviz changes depending on the pr
- python 3x - about the calculation of cross entropy in pytorch
- python contour detection¢roid calculation
- multiple classification score calculation errors, python, svm
- python - how to convert exponential notation to decimal notation
- python - about the average value calculation method of mass calculation
- python - pass the calculation defined by the function for all combinations of arrays
- python time calculation
- python 3x - calculation method of distance between data using log-likelihood
- python - i want to calculate matrix calculation with numpy for statement with numpydot
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- xcode - pod install [!] no `podfile 'found in the project directory