Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110893
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Christopher Reimer <christopher_reimer@icloud.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Creating a calculator |
| Date | Fri, 01 Jul 2016 05:25:58 -0700 |
| Lines | 68 |
| 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> |
| Mime-Version | 1.0 (1.0) |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | news.uni-berlin.de uopFiOeNOP1W4pyydQ1TTgvTKnZjDe+6dlGBX4YKg0QQ== |
| Return-Path | <christopher_reimer@icloud.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'broken': 0.03; 'operator': 0.03; 'discard': 0.05; 'assignment': 0.07; 'cc:addr:python-list': 0.09; 'expected.': 0.09; 'happens.': 0.09; 'interpreter,': 0.09; 'splitting': 0.09; 'tab': 0.09; 'unpack': 0.09; 'ignore': 0.14; '11:42': 0.16; 'calculator': 0.16; 'crashes': 0.16; 'dfs': 0.16; 'expected,': 0.16; 'keyword,': 0.16; 'rather,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'try/except': 0.16; 'wrote:': 0.16; 'later': 0.16; 'element': 0.18; 'input': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'latter': 0.22; 'parse': 0.22; 'strict': 0.22; 'cc:no real name:2**0': 0.22; 'seems': 0.23; 'elements': 0.23; 'split': 0.23; 'this:': 0.23; 'second': 0.24; 'skip:- 40': 0.25; "doesn't": 0.26; 'chris': 0.26; 'received:17': 0.27; 'bad.': 0.29; 'crash': 0.29; 'end,': 0.29; 'silence': 0.29; 'strings,': 0.29; 'whitespace': 0.29; 'print': 0.30; 'code': 0.30; 'entry': 0.31; 'related': 0.32; 'run': 0.33; 'third': 0.33; 'list': 0.34; 'formats': 0.35; 'but': 0.36; 'needed': 0.36; 'there': 0.36; 'basic': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'charset:us-ascii': 0.37; 'test': 0.39; 'data': 0.39; 'where': 0.40; 'space': 0.40; 'header :MIME-version:1': 0.60; 'skip:u 10': 0.61; 'skip:n 10': 0.62; '30,': 0.63; 'more': 0.63; 'compact,': 0.84; 'header:In-reply- to:1': 0.84; 'pays.': 0.84; 'lot,': 0.95 |
| X-Proofpoint-Virus-Version | vendor=fsecure engine=2.50.10432:,, definitions=2016-07-01_04:,, signatures=0 |
| X-Proofpoint-Spam-Details | rule=notspam policy=default score=0 spamscore=0 clxscore=1011 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1510270003 definitions=main-1607010121 |
| X-Mailer | iPhone Mail (13F69) |
| In-reply-to | <lf5oa6hhn9x.fsf@ling.helsinki.fi> |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=4d515a; t=1467375960; bh=ObrZ8Qg0IR01AZCsUCIXlknsCPIfiwUl4Sz6PhnsXE0=; h=Content-type:MIME-version:Subject:From:Date:Message-id:To; b=Xi2P4tA/Nr/AiCp2AGKHd1BzD+viUWU4qwlkiII20+0xeRqerLZnc1nowOK2DHc1b VEhqCtxwxmjeiCi7+IZ/dOCHc57Mc7FFpCHyU4tRrITfK1yX9m0DPVdguQ86Pzkafa wSukVl2Wr+Er0qSma0m4GWYpN5ENfWm/PvOjjfWfwAwg+fzwczK2g90QKQcwdk2pO4 4UtXPzFoNeCKlPKMSOlJ37ffnTSXHp7WlVjxl/lazoj9jbgTsTmyJJKK3M6/sZ4E7a O4f+Y7pfeeNkYBiort3oogiQ8kgCk/jl+bXTXF+lgxOgyc/GKNMT2tTDNZjD4C/pEC gn2NHaiWmuDHw== |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <0E77E5BC-E8CC-4951-B520-37BF2E596D3A@icloud.com> |
| X-Mailman-Original-References | <a0ed0591-dcbb-4ad2-b69a-58feef50153a@googlegroups.com> <nl4t1n$qgp$2@dont-email.me> <lf5oa6hhn9x.fsf@ling.helsinki.fi> |
| Xref | csiph.com comp.lang.python:110893 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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