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


Groups > comp.lang.python > #110892

Re: Creating a calculator

Path csiph.com!feeder.erje.net!2.eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Warrick <kwpolska@gmail.com>
Newsgroups comp.lang.python
Subject Re: Creating a calculator
Date Fri, 1 Jul 2016 13:39:39 +0200
Lines 42
Message-ID <mailman.155.1467373181.2358.python-list@python.org> (permalink)
References <a0ed0591-dcbb-4ad2-b69a-58feef50153a@googlegroups.com> <nl4t1n$qgp$2@dont-email.me> <1mpph6r.u6n05219srlzsN%pdorange@pas-de-pub-merci.mac.com> <CAMw+j7LLFVBB_bkX5o3DD_yP_Y+hbN+KX_zWUKFMaYAuKRFMTA@mail.gmail.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 <kwpolska@gmail.com>
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 <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 <CAMw+j7LLFVBB_bkX5o3DD_yP_Y+hbN+KX_zWUKFMaYAuKRFMTA@mail.gmail.com>
X-Mailman-Original-References <a0ed0591-dcbb-4ad2-b69a-58feef50153a@googlegroups.com> <nl4t1n$qgp$2@dont-email.me> <1mpph6r.u6n05219srlzsN%pdorange@pas-de-pub-merci.mac.com>
Xref csiph.com comp.lang.python:110892

Show key headers only | View raw


On 1 July 2016 at 11:34, Pierre-Alain Dorange
<pdorange@pas-de-pub-merci.mac.com> wrote:
> DFS <nospam@dfs.com> 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 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))
>> -----------------------------------------------
>
> 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 course...

No, this is awful. It’s a great way to compromise your system’s
security. Never use eval() for any reason, especially with user input
— if you were to type in __import__('os').system('…') with some
particularly dangerous command (rm, format, …), you would kill your
system.

-- 
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16

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