#๐ Making an interpreter in python and having an issue with mathmatical operations
17 messages ยท Page 1 of 1 (latest)
@full canyon
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Whenever i do the equation (5 + 5/5) * 5 * -10 it does not calculate the last multiplication it ends up just doing 5 + 5/5 and multiplying 5 and -10 making values be the list [6,-50] and returning 6
at line 127 or so - I think one of the issues is that you're incrementing open_brackets more than once when it encounters a (
you set it to 1 at line 130, and then the first if in the while will increment it a second time, because that token is still (
actually, that might not be it, as you do increment the token marker i after the first (
suggested debug: at the start of evaluate(), print out expression to ensure it is what you think it is
ok ill try that out really quick\
so this is whats happening
Tokens: ['(', '5', '+', '5', '/', '5', ')', '', '5', '', '-10']
(5 + 5/ 5) * 5 * -10
Tokens: ['5', '+', '5', '/', '5']
5 + 5 / 5
Operators after evaluation: []
Values after evaluation: [6.0]
Operators after evaluation: []
Values after evaluation: [6.0, -50.0]
at some point it calculates 5 * -10 but it never has the expression be 6 * 5 * -10
@twin sigil do you think my stacks arent implemented properly
well, it sounds like the way things are being coaelesced isn't working right
take a look at that book I recommended, as they discuss how to implement stuff like this and many of the subtleties involved
and how things like recursive evaluation of expressions can work
ok thank you
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.