[Tutor] python arthematics
ALAN GAULD
alan.gauld at btinternet.com
Fri Mar 23 01:46:29 CET 2012
Please use ReplyAll when responding to posts, Thanks.
i have tried like this way but i want to read all integers and operators in one string. how can i do that.
>
>
>As described below.
>
>
>expr = raw_input("Type an expression: ")
>
>
>Now you have the expression as a string in expr.
>You just need to analyze it to identify the various components (this is called parsing).
>
>
>One simplistic approach to get you started is to treat white-space and the operators
>as separators between operands. You can then scan the string to extract a list of
>operands and a list of operators. So " 3 + 4 * 6" yields:
>
>
>operators = ["+", "*"]
>operands = ["3", "4", "6"]
>
>
>Since we are expecting the operands to be integers we can go further
>
>
>operands = [int(n) for n in operands]
>
>
>For the operators you need to do a lookup table and maybe map them
>to the operators module operations...
>
>
>You can then start matching them and performing the operations just like you did
>
>in your code with the values read separately. eg. You can apply each operator in
>turn to the first pair of operands in the list:
>
>
>3 + 4 -> 7
>7 * 6 -> 42
>
>That doesn't take any account of the normal rules of precedence and
>thats when things start to get much more complex. Also it doesn't allow
>for parenthesised sub expressions etc. This is actually quite tricky to get right,
>but a very good learning exercise! (But much harder than you probably
>thought it would be!)
>
>
>HTH
>
>
>
>________________________________
> From: Alan Gauld <alan.gauld at btinternet.com>
>To: tutor at python.org
>Sent: Thursday, 22 March 2012 8:14 PM
>Subject: Re: [Tutor] python arthematics
>
>On 22/03/12 22:14, Sukhpreet Sdhu wrote:
>> i want to write a program that reads simple arithematic expressions and
>> calculates the result.
>
>OK, there are many ways to do this but they pretty much
>fall into three categories:
>
>1) read the string and exec() it - easy but very
> risky from a security point of view.
>
>2) Write a general text parser to break the input
> string into operands and operators and combine
> the results.
>
>3) build a state machine that looks for valid input
> (this will be easier if you opt for reverse
polish
> notation BTW)
>
>You can do No 1 real quick just for fun, but don't try
>that in a real world program.
>
>So that leaves 2 and 3. If you understand the concepts
>(or research them on wikipedia) you can have at it,
>write some code and lets see what you get to.
>Hint: Start with a limited subset - only 2 operands and
>one operator allowed, say.
>
>HTH
>-- Alan G
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>
>_______________________________________________
>Tutor maillist - Tutor at python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120323/82899aaa/attachment.html>
More information about the Tutor
mailing list