Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #110893

Re: Creating a calculator

From Christopher Reimer <christopher_reimer@icloud.com>
Newsgroups comp.lang.python
Subject Re: Creating a calculator
Date 2016-07-01 05:25 -0700
Message-ID <mailman.156.1467375975.2358.python-list@python.org> (permalink)
References <a0ed0591-dcbb-4ad2-b69a-58feef50153a@googlegroups.com> <nl4t1n$qgp$2@dont-email.me> <lf5oa6hhn9x.fsf@ling.helsinki.fi> <0E77E5BC-E8CC-4951-B520-37BF2E596D3A@icloud.com>

Show all headers | View raw


> On Jun 30, 2016, at 11:42 PM, Jussi Piitulainen <jussi.piitulainen@helsinki.fi> 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.
>> 
>> -----------------------------------------------
>> ui=raw_input('Enter calculation to perform: ')
>> n1=float(ui.split(' ')[0])
>> op=ui.split(' ')[1]
>> n2=float(ui.split(' ')[2])
>> if op=='+':c=n1+n2
>> if op=='-':c=n1-n2
>> if op=='*':c=n1*n2
>> if op=='/':c=n1/n2
>> print(ui+' = '+str(c))
>> -----------------------------------------------
> 
> I use multiple assignment a lot, like this:
> 
>    n1, op, n2 = ui.split()
> 
> It's not only compact, it also crashes if there are more elements than
> expected, and I want it to crash when that happens. Or rather, I prefer
> a crash to silence when input is bad.
> 
> For the calculator, it may be better to split on any whitespace and
> discard empty strings, which is what ui.split() does. Splitting on a
> single space seems unnecessarily strict in a calculator (whereas
> splitting on a single tab is what I very much do in my work - the data
> formats are such).
> 
> I think multiple assignment is good even for a beginner. Perhaps do it a
> second time straight away:
> 
>    n1, op, n2 = ui.split()
>    n1, n2 = float(n1), float(n2)
> 
> But it's only with the split where it really pays.
> 
>    n1, op, n2 = ui.split()
>    n1 = float(n1)
>    n2 = float(n2)
> 
> The latter might be even preferable. Hm.
> 
>    n1, n2 = map(float, (n1, n2))
> 
> :)

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 element for expression, an empty list is assigned.

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 will test the expression, ignore if empty or run regex if full. 

Chris R.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Creating a calculator Elizabeth Weiss <cake240@gmail.com> - 2016-06-30 20:08 -0700
  Re: Creating a calculator Michael Torrie <torriem@gmail.com> - 2016-06-30 21:38 -0600
  Re: Creating a calculator DFS <nospam@dfs.com> - 2016-06-30 23:57 -0400
  Re: Creating a calculator DFS <nospam@dfs.com> - 2016-07-01 00:54 -0400
    Re: Creating a calculator Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-07-01 09:42 +0300
      Re: Creating a calculator Christopher Reimer <christopher_reimer@icloud.com> - 2016-07-01 05:25 -0700
        Re: Creating a calculator Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-07-01 15:46 +0300
          Re: Creating a calculator Christopher Reimer <christopher_reimer@icloud.com> - 2016-07-01 06:19 -0700
            Re: Creating a calculator Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-07-01 16:35 +0300
        Re: Creating a calculator Steven D'Aprano <steve@pearwood.info> - 2016-07-01 23:52 +1000
          Re: Creating a calculator alister <alister.ware@ntlworld.com> - 2016-07-01 14:21 +0000
          Re: Creating a calculator Christopher Reimer <christopher_reimer@icloud.com> - 2016-07-01 07:15 -0700
          Re: Creating a calculator Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-07-01 20:44 +0300
          Re: Creating a calculator Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-07-04 12:32 +0200
      Re: Creating a calculator DFS <nospam@dfs.com> - 2016-07-01 20:16 -0400
    Re: Creating a calculator pdorange@pas-de-pub-merci.mac.com (Pierre-Alain Dorange) - 2016-07-01 11:34 +0200
      Re: Creating a calculator Chris Warrick <kwpolska@gmail.com> - 2016-07-01 13:39 +0200
        Re: Creating a calculator pdorange@pas-de-pub-merci.mac.com (Pierre-Alain Dorange) - 2016-07-01 15:03 +0200
      Re: Creating a calculator DFS <nospam@dfs.com> - 2016-07-01 20:16 -0400
        Re: Creating a calculator pdorange@pas-de-pub-merci.mac.com (Pierre-Alain Dorange) - 2016-07-04 11:50 +0200
        Re: Creating a calculator BartC <bc@freeuk.com> - 2016-07-06 01:53 +0100
          Re: Creating a calculator Quivis <quivis@domain.invalid> - 2016-07-07 22:00 +0000
  Re: Creating a calculator Chris Warrick <kwpolska@gmail.com> - 2016-07-01 10:57 +0200

csiph-web