Re: Creating a calculator

2016-07-05 Thread BartC
On 02/07/2016 01:16, DFS wrote: On 7/1/2016 5:34 AM, Pierre-Alain Dorange wrote: More reduced : -- u=raw_input('Enter calculation:") print eval(u) -- works and compute : 1+2+3+4-1+4*2 2+3.0/2-0.5 Perform better and shorter, but l

Re: Creating a calculator

2016-07-04 Thread Antoon Pardon
Op 01-07-16 om 15:52 schreef Steven D'Aprano: > On Fri, 1 Jul 2016 10:25 pm, Christopher Reimer wrote: > >> For my BASIC interpreter, each line of BASIC is broken this way into >> tokens. > [...] >> By using * to unpack the split line, my program no longer crashes and no >> try/except block is need

Re: Creating a calculator

2016-07-04 Thread Pierre-Alain Dorange
DFS wrote: > > 2 lines? Love it! > > But apparently eval==evil. > > http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html > > I bet you get hammered about it here on clp. It was a software to be deploy, it was just for educational purpose. -- Pierre-Alain Dorange

Re: Creating a calculator

2016-07-01 Thread Jussi Piitulainen
Steven D'Aprano writes: > On Fri, 1 Jul 2016 10:25 pm, Christopher Reimer wrote: > >> For my BASIC interpreter, each line of BASIC is broken this way into >> tokens. > [...] >> By using * to unpack the split line, my program no longer crashes and no >> try/except block is needed to work around the

Re: Creating a calculator

2016-07-01 Thread Christopher Reimer
> On Jul 1, 2016, at 6:52 AM, Steven D'Aprano wrote: > >> On Fri, 1 Jul 2016 10:25 pm, Christopher Reimer wrote: >> >> For my BASIC interpreter, each line of BASIC is broken this way into >> tokens. > [...] >> By using * to unpack the split line, my program no longer crashes and no >> try/except

Re: Creating a calculator

2016-07-01 Thread alister
On Fri, 01 Jul 2016 23:52:45 +1000, Steven D'Aprano wrote: > On Fri, 1 Jul 2016 10:25 pm, Christopher Reimer wrote: > >> For my BASIC interpreter, each line of BASIC is broken this way into >> tokens. > [...] >> By using * to unpack the split line, my program no longer crashes and >> no try/excep

Re: Creating a calculator

2016-07-01 Thread Steven D'Aprano
On Fri, 1 Jul 2016 10:25 pm, Christopher Reimer wrote: > For my BASIC interpreter, each line of BASIC is broken this way into > tokens. [...] > By using * to unpack the split line, my program no longer crashes and no > try/except block is needed to work around the crash. A later line of code > wil

Re: Creating a calculator

2016-07-01 Thread Jussi Piitulainen
Christopher Reimer writes: >> On Jul 1, 2016, at 5:46 AM, Jussi Piitulainen wrote: >> >> Christopher Reimer writes: >> >>> For my BASIC interpreter, each line of BASIC is broken this way into >>> tokens. >>> >>> line_number, keyword, *expression = line.split(' ', 2) >>> >>> For a line like 10

Re: Creating a calculator

2016-07-01 Thread Christopher Reimer
> On Jul 1, 2016, at 5:46 AM, Jussi Piitulainen > wrote: > > Christopher Reimer writes: > >> For my BASIC interpreter, each line of BASIC is broken this way into >> tokens. >> >> line_number, keyword, *expression = line.split(' ', 2) >> >> For a line like 10 PRINT "HELLO, WORLD!", this works

Re: Creating a calculator

2016-07-01 Thread Pierre-Alain Dorange
Chris Warrick wrote: > > More reduced : > > -- > > u=raw_input('Enter calculation:") > > print eval(u) > > -- > > works and compute : > > 1+2+3+4-1+4*2 > > 2+3.0/2-0.5 > > > > Perform better and shorter, but less educationnal of cour

Re: Creating a calculator

2016-07-01 Thread Jussi Piitulainen
Christopher Reimer writes: > For my BASIC interpreter, each line of BASIC is broken this way into > tokens. > > line_number, keyword, *expression = line.split(' ', 2) > > For a line like 10 PRINT "HELLO, WORLD!", this works as expected. > > For a line like 20 END, which doesn't have a third elemen

Re: Creating a calculator

2016-07-01 Thread Christopher Reimer
> On Jun 30, 2016, at 11:42 PM, Jussi Piitulainen > wrote: > > DFS writes: > >> Here's a related program that doesn't require you to tell it what type >> of operation to perform. Just enter 'num1 operator num2' and hit >> Enter, and it will parse the entry and do the math. >> >> -

Re: Creating a calculator

2016-07-01 Thread Chris Warrick
On 1 July 2016 at 11:34, Pierre-Alain Dorange wrote: > DFS wrote: > >> Here's a related program that doesn't require you to tell it what type >> of operation to perform. Just enter 'num1 operator num2' and hit Enter, >> and it will parse the entry and do the math. >> >> -

Re: Creating a calculator

2016-07-01 Thread Pierre-Alain Dorange
DFS wrote: > Here's a related program that doesn't require you to tell it what type > of operation to perform. Just enter 'num1 operator num2' and hit Enter, > and it will parse the entry and do the math. > > --- > ui=raw_input('Enter calculation to p

Re: Creating a calculator

2016-07-01 Thread Chris Warrick
On 1 July 2016 at 05:08, Elizabeth Weiss wrote: > while True: > print("Options:") > print("Enter 'add' to add two numbers") > print("Enter 'subtract' to subtract two numbers") > print("Enter 'multiply' to multiply two numbers") > print("Enter 'divide' to div

Re: Creating a calculator

2016-06-30 Thread Jussi Piitulainen
DFS writes: > Here's a related program that doesn't require you to tell it what type > of operation to perform. Just enter 'num1 operator num2' and hit > Enter, and it will parse the entry and do the math. > > --- > ui=raw_input('Enter calculation to pe

Re: Creating a calculator

2016-06-30 Thread Michael Torrie
On 06/30/2016 09:08 PM, Elizabeth Weiss wrote: > while True: > print("Options:") > print("Enter 'add' to add two numbers") > print("Enter 'subtract' to subtract two numbers") > print("Enter 'multiply' to multiply two numbers") > print("Enter 'divide' to divide two numb

Creating a calculator

2016-06-30 Thread Elizabeth Weiss
while True: print("Options:") print("Enter 'add' to add two numbers") print("Enter 'subtract' to subtract two numbers") print("Enter 'multiply' to multiply two numbers") print("Enter 'divide' to divide two numbers") print("Enter 'quit' to end the prog