You are trying to evaluate an arithmetic expression. There are several levels of understanding of your question.
1) If you only want a working solution, you can use python's own expression evaluator:
result = eval(' '.join(str(x) for x in mylist))
this is a little bit dangerous if somebody else can send a list to your program: it could be used to evaluate arbitrary code on your computer.
2) If you are writing your own expression mini-language and your intention is to write expressions which the python interpreter does not understand, then you need a parser which is exactly this: a machine to evaluate expressions obeying a certain grammar. I recommend using wisent in this case. It is a very simple and efficient parser. But there are many similar libraries in python.
3) You may also develop your own method for parsing expressions. You can probably design a method for simple expressions such as the one in your question. Try to use list operations and tests to determine what to do. However, if you want to extend this to more complex expressions, you will have to learn a bit of the theory of parsing languages, which is a well known and well understood domain. Don't reinvent the wheel!
Edited 1 Year Ago by Gribouillis
Thank you @Gribouillis. The code you gave was the exact thing i was looking for. Of course i will work more on parser and wisent. And also will try to develop my own method.
Now here i have a problem. Look at this:
mylist = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2] result = eval(' '.join(str(x) for x in mylist)) print result
If you run this you will get 19, the result will be set to 19.
If you use your pc calculator and enter this:
And then press
= button, calculator will show you number 19.5 as the result.
But if you use your pc calculator and enter like this:
And then press
= button and after that enter +9 and again press
= button and go on, the last number that calculator will show you will be number 10.
I know what is the reason. Calculator do operation step by step when we add numbers step to step into calculator, but when we enter all the numbers and signs togather; calculator run multiplication first, after that division, then addition and finally subtraction. So the result will be different.
Now as i'm creating a simple calculator and using the code i put above, how can i fix this problem. With the code above, my calculator will run operations with this rule
*/+- and the result will be number 19. How can i get the result as number 10.
I don't know i explained my meaning clear enough or not.
And let me explain something more. in my calculator (wich is a Tkinter button) when we press a button of a number, it's number will be append into the list, then we press a operator button and the sign will be append to the list and go on. Finally when we press
= button, the code i posted in my last post, will run to calculate the list and print the result.
Use () to change the order:
mylist = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2] result = eval(' '.join(str(x) for x in mylist)) print(result) # --> 19 mylist = ['(', 6, '/', 3, '+', 9, '+', 8, '+', 1, ')', '/', 2] result = eval(' '.join(str(x) for x in mylist)) print(result) # --> 10
Very basic math rule!
Edited 1 Year Ago by HiHe
Yes @HiHe i know that. But the problem is that i'm trying to create a simple calculator. Every time i press a number-button, it's number will be appended to a list. And finally when i press
= button, the list will go to run all math operation i have enterd their operator signs into that list. So i can't put any
() into my list.
Or..... maybe i could define a method of mine for that! I will work on this part.
Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...
Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.
This is the result of previous question answered :
code for the listbox - datagridview interaction
At the top of the code ...
I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...