Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110892
| From | Chris Warrick <kwpolska@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Creating a calculator |
| Date | 2016-07-01 13:39 +0200 |
| 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> |
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 | 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