Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Christopher Reimer Newsgroups: comp.lang.python Subject: Re: Creating a calculator Date: Fri, 01 Jul 2016 05:25:58 -0700 Lines: 68 Message-ID: References: <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: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <0E77E5BC-E8CC-4951-B520-37BF2E596D3A@icloud.com> X-Mailman-Original-References: Xref: csiph.com comp.lang.python:110893 > On Jun 30, 2016, at 11:42 PM, Jussi Piitulainen wrote: >=20 > DFS writes: >=20 >> 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. >>=20 >> ----------------------------------------------- >> ui=3Draw_input('Enter calculation to perform: ') >> n1=3Dfloat(ui.split(' ')[0]) >> op=3Dui.split(' ')[1] >> n2=3Dfloat(ui.split(' ')[2]) >> if op=3D=3D'+':c=3Dn1+n2 >> if op=3D=3D'-':c=3Dn1-n2 >> if op=3D=3D'*':c=3Dn1*n2 >> if op=3D=3D'/':c=3Dn1/n2 >> print(ui+' =3D '+str(c)) >> ----------------------------------------------- >=20 > I use multiple assignment a lot, like this: >=20 > n1, op, n2 =3D ui.split() >=20 > 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. >=20 > 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). >=20 > I think multiple assignment is good even for a beginner. Perhaps do it a > second time straight away: >=20 > n1, op, n2 =3D ui.split() > n1, n2 =3D float(n1), float(n2) >=20 > But it's only with the split where it really pays. >=20 > n1, op, n2 =3D ui.split() > n1 =3D float(n1) > n2 =3D float(n2) >=20 > The latter might be even preferable. Hm. >=20 > n1, n2 =3D map(float, (n1, n2)) >=20 > :) For my BASIC interpreter, each line of BASIC is broken this way into tokens.= line_number, keyword, *expression =3D 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, a= n 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 t= est the expression, ignore if empty or run regex if full.=20 Chris R.=