Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110876 > unrolled thread
| Started by | Elizabeth Weiss <cake240@gmail.com> |
|---|---|
| First post | 2016-06-30 20:08 -0700 |
| Last post | 2016-07-01 10:57 +0200 |
| Articles | 3 on this page of 23 — 12 participants |
Back to article view | Back to comp.lang.python
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
Page 2 of 2 — ← Prev page 1 [2]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2016-07-06 01:53 +0100 |
| Message-ID | <nlhkqo$drh$1@dont-email.me> |
| In reply to | #110922 |
On 02/07/2016 01:16, DFS wrote:
> On 7/1/2016 5:34 AM, Pierre-Alain Dorange wrote:
>
>
>> 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...
>
>
> 2 lines? Love it!
That's not really implementing a calculator. It's just feeding the input
to Python to execute immediately. I can reduce it to zero lines: start
Python so that the >>> prompt appears. Then type in:
>>> 2+3.0/2-0.5
and so on. No other Python code is needed.
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Quivis <quivis@domain.invalid> |
|---|---|
| Date | 2016-07-07 22:00 +0000 |
| Message-ID | <EhAfz.76080$H_.5373@fx36.am4> |
| In reply to | #111160 |
On Wed, 06 Jul 2016 01:53:36 +0100, BartC wrote:
> That's not really implementing a calculator. It's just feeding the input
> to Python to execute immediately. I can reduce it to zero lines: start
> Python so that the >>> prompt appears. Then type in:
>
> >>> 2+3.0/2-0.5
>
> and so on. No other Python code is needed.
Or do it like:
$ python -c 'print 2+3.0/2-0.5'
3.0
--
_____ __ __ __ __ __ __ __
(( )) || || || \\ // || ((
\\_/X| \\_// || \V/ || \_))
Omnia paratus *~*~*~*~*~*~*
[toc] | [prev] | [next] | [standalone]
| From | Chris Warrick <kwpolska@gmail.com> |
|---|---|
| Date | 2016-07-01 10:57 +0200 |
| Message-ID | <mailman.154.1467363433.2358.python-list@python.org> |
| In reply to | #110876 |
On 1 July 2016 at 05:08, Elizabeth Weiss <cake240@gmail.com> wrote:
> while True:
> print("Options:")
> print("Enter 'add' to add two numbers")
> print("Enter 'subtract' to subtract two numbers")
> print("Enter 'multiply' to multiply two numbers")
> print("Enter 'divide' to divide two numbers")
> print("Enter 'quit' to end the program")
> user_input=input(":")
> if user_input=="quit":
> break
> elif user_input=="add":
> num1=float(input("Enter a number"))
> num2=float(input("Enter another number"))
> result=str(num1+num2)
> print("The answer is"+ result)
> elif user_input=="subtract":
> num1=float(input("Enter a number"))
> num2=float(input("Enter another number"))
> result=str(num1-num2)
> print("The answer is"+result)
>
> Two questions:
> 1. Why do I need to put ' ' around the words add, subtract, multiply, quit, etc. when it is already in quotes in print()? When the calculator asks me which option I would like to choose I do not write 'add'- I only write add.
This is used for display. The single quotes will be displayed as part
of the string. This is so that people notice the commands, for
example.
>>> print("Enter 'add' to add two numbers")
Enter 'add' to add two numbers
>>> print("Enter add to add two numbers")
Enter add to add two numbers
>>> print('Enter "add" to add two numbers')
Enter "add" to add two numbers
> 2. The program I am using to help me learn python mentions that the output line could be put outside the if statements to omit repetition of code. What does this mean and how would I write the code differently according to this?
Look at your current code. The following three lines appear twice (and
will appear 4 times if you add multiplication and division):
> num1=float(input("Enter a number"))
> num2=float(input("Enter another number"))
> print("The answer is"+ result)
This is code repetition. It’s discouraged, because if you have 4
copies of a longer segment, and you want to change something, you
would have to remember to change it in 4 places. In this case, you can
avoid code reuse like this:
1. Check if user said 'quit', and if yes, break from the loop. (Ignore
invalid input for now)
2. Ask the user for two numbers.
3. Make an if/elif/else structure to calculate the result.
4. Print out the result outside of `if`.
Example for 3. and 4.:
if user_input == 'add':
result = num1 + num2 # no need to call str() if you use commas in print()
elif user_input == 'subtract':
result = num1 - num2
# other elif clauses go here
print("The result is", result)
--
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.python
csiph-web