Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110878
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Michael Torrie <torriem@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Creating a calculator |
| Date | Thu, 30 Jun 2016 21:38:16 -0600 |
| Lines | 54 |
| Message-ID | <mailman.151.1467344302.2358.python-list@python.org> (permalink) |
| References | <a0ed0591-dcbb-4ad2-b69a-58feef50153a@googlegroups.com> <8b950351-a04f-1b5e-87dd-8a6a244e05d8@gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252 |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de x+8ijePtRYqb5z+2spp2JgcEYyQ/cniAi9xkVA8mggyA== |
| Return-Path | <torriem+gmail@torriefamily.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'elif': 0.04; 'true,': 0.04; 'differently': 0.07; 'omit': 0.07; 'mentions': 0.09; 'result)': 0.09; 'statements': 0.09; 'subtract': 0.09; 'python': 0.10; 'output': 0.13; 'add,': 0.16; 'add.': 0.16; 'calculator': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'helps.': 0.16; 'quoted': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'screen,': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'string': 0.17; 'say,': 0.18; 'stick': 0.18; 'input': 0.18; 'runs': 0.18; '>>>': 0.20; 'posted': 0.21; 'questions:': 0.22; 'code.': 0.23; 'examples': 0.24; 'words': 0.24; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'least': 0.27; 'said,': 0.27; 'skip:u 20': 0.28; 'asks': 0.29; 'screen.': 0.29; 'character': 0.29; 'print': 0.30; 'books': 0.30; 'code': 0.30; 'regardless': 0.31; 'option': 0.31; 'another': 0.32; 'knows': 0.32; 'screen': 0.32; 'quotes': 0.33; 'message- id:@gmail.com': 0.34; 'this?': 0.34; 'add': 0.34; 'could': 0.35; 'something': 0.35; "isn't": 0.35; 'according': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'received:org': 0.37; 'wanted': 0.37; 'things': 0.38; 'mean': 0.38; 'end': 0.39; 'means': 0.39; 'why': 0.39; 'does': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'hope': 0.61; 'skip:u 10': 0.61; 'real': 0.62; 'charset:windows-1252': 0.62; 'strictly': 0.64; 'stand': 0.67; 'choose': 0.68; 'elizabeth': 0.72; 'special': 0.73; 'computation.': 0.84; 'divide': 0.84; 'each,': 0.84; 'surround': 0.84; 'british': 0.88; '"how': 0.91 |
| X-Virus-Scanned | amavisd-new at torriefamily.org |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 |
| In-Reply-To | <a0ed0591-dcbb-4ad2-b69a-58feef50153a@googlegroups.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 | <8b950351-a04f-1b5e-87dd-8a6a244e05d8@gmail.com> |
| X-Mailman-Original-References | <a0ed0591-dcbb-4ad2-b69a-58feef50153a@googlegroups.com> |
| Xref | csiph.com comp.lang.python:110878 |
Show key headers only | View raw
On 06/30/2016 09:08 PM, Elizabeth Weiss 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.
I think those extra quotes are just to make the output stand out when
it's printed to the screen so that the user knows that the word in
quotes is the special word they will need to type in. It's much like
how things are quoted in literature.
Since the string is already inside of double quotes, the inner single
quotes simply get printed. And if you wanted to print real double
quotes to the screen, you can surround the string with single quotes.
Either works, depending on which quote character you want to actually
print to the screen.
Here's examples using the interactive python prompt:
>>> print ('He said, "How are you?"')
He said, "How are you?"
>>> print ("In British books one might say, 'How are you?'")
In British books one might say, 'How are you?'
Hope this helps.
>
> 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?
It means that since each of the elif blocks does something that needs to
be printed out, and if it's being printed out the same way by each, then
you can just stick it at the end of the if processing block so that it
always runs regardless of which elif block did the computation. Though
in your code example this isn't strictly true, since if the input was
not "add" or "subtract" then there is no result to print. At least if
the code you posted was complete.
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