Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Warrick Newsgroups: comp.lang.python Subject: Re: Creating a calculator Date: Fri, 1 Jul 2016 13:39:39 +0200 Lines: 42 Message-ID: References: <1mpph6r.u6n05219srlzsN%pdorange@pas-de-pub-merci.mac.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de VNUV6w0gGjg/JBiOeV1RrAQTNCYSD9ihSUPM0CHTzi3Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'operator': 0.03; 'reason,': 0.07; 'cc:addr:python-list': 0.09; 'compute': 0.09; 'security.': 0.09; '\xe2\x80\x94': 0.09; '2016': 0.16; 'dfs': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'pfxlen:0': 0.18; 'input': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'parse': 0.22; 'cc:no real name:2**0': 0.22; 'header:In-Reply-To:1': 0.24; 'skip:- 40': 0.25; "doesn't": 0.26; 'command': 0.26; 'chris': 0.26; 'message- id:@mail.gmail.com': 0.27; 'format,': 0.27; 'it\xe2\x80\x99s': 0.29; 'reduced': 0.29; 'print': 0.30; 'entry': 0.31; 'especially': 0.32; 'skip:- 30': 0.32; 'related': 0.32; 'skip:_ 30': 0.33; 'received:google.com': 0.35; 'but': 0.36; 'subject:: ': 0.37; 'no,': 0.38; 'system.': 0.39; 'some': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'skip:n 10': 0.62; 'more': 0.63; 'great': 0.63; 'dangerous': 0.70; 'course...': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=9+jz4/xjocnVO7+hko0aH2Iw25SkgxSM/Ce9u6KyRsM=; b=jJwYZP/8vn9/biqkmcZCCLDJ+OKuC5BXj+l7w0yVpMGxQhbgrHhBSJsvk4YZ7lJTsV u9ydKqtWwZVd6JKgEC1G1d21OSfcuegSt2LZIVMqDkDvH3oRfkScu2FVUs7AMmmf/Kmn FCqO/Dmvls2buhkrO05j0gvViE3CKB8qmTgGaPENl8H/eOSJHm474cQCIZNJJjfTTjGu D9IfTTmewbBZQ8tV9UyrFBIZ+FR3ZXqP6CxNLorC/2M8iD6kWrfIG3+4ebvf5XA54smb dXVM3QBE7QLq6GR6djHtikKi1StPfBxLz+9gZ553C2j/IR9vW7isGal9gX0qzPHNgii+ LL4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=9+jz4/xjocnVO7+hko0aH2Iw25SkgxSM/Ce9u6KyRsM=; b=Jlmh0nGrj8XQciQlrEd0ZzRNxpk+y3x1JgNO/2lLTcOQv6e9IVqWMsIAxyrUKJhp2E boOzEPC+LJ8YA8cnAZjb9mDhKhSSTd5RWh2ztKR72ANVASkWEGNbo8frtdB0L5+BEAfz FCAKfCGl/1fSDN0boNnJUrYnIOosiDFNyvVjhtgwpVbz78m7cX7IRuvJ2lETpUMHdl8v GMdzM2URPh26/VuUqJrPYU0FYC7rtzYZwK31ndMGeIIw+dDJWr8x3yqpuy7GHGl9Whvh 8sy50J/Wt1PL125o3Oa+yhsegYucXps32pCGzTPMBwUaeSgCLe5WBY+MOs88FwRuXRx9 +PFA== X-Gm-Message-State: ALyK8tLOlUszWxefuixupMYz6WaCBMsvIwfLHBIWmcg7ybesGKxWquwAuFkPej6ol/LtXr3I4M+gXSSFQ5boOw== X-Received: by 10.194.177.230 with SMTP id ct6mr3139964wjc.170.1467373179928; Fri, 01 Jul 2016 04:39:39 -0700 (PDT) In-Reply-To: <1mpph6r.u6n05219srlzsN%pdorange@pas-de-pub-merci.mac.com> 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: X-Mailman-Original-References: <1mpph6r.u6n05219srlzsN%pdorange@pas-de-pub-merci.mac.com> Xref: csiph.com comp.lang.python:110892 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. >> >> ----------------------------------------------- >> 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)) >> ----------------------------------------------- > > More reduced : > ---------------------------------- > u=3Draw_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 course... No, this is awful. It=E2=80=99s a great way to compromise your system=E2=80= =99s security. Never use eval() for any reason, especially with user input =E2=80=94 if you were to type in __import__('os').system('=E2=80=A6') with = some particularly dangerous command (rm, format, =E2=80=A6), you would kill your system. --=20 Chris Warrick PGP: 5EAAEA16