Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110889
| Path | csiph.com!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 10:57:11 +0200 |
| Lines | 74 |
| Message-ID | <mailman.154.1467363433.2358.python-list@python.org> (permalink) |
| References | <a0ed0591-dcbb-4ad2-b69a-58feef50153a@googlegroups.com> <CAMw+j7J5916Pz2TuYU9d+N3Eg+CuB16iERZA-+6x6th71sQ4LQ@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 LWhQSEqR9beC21xkb/niCACOg5SGO3qkOWWPiGvsP1UA== |
| Return-Path | <kwpolska@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'elif': 0.04; 'string.': 0.04; 'differently': 0.07; 'omit': 0.07; 'cc:addr:python-list': 0.09; 'loop.': 0.09; 'mentions': 0.09; 'result)': 0.09; 'statements': 0.09; 'subtract': 0.09; 'python': 0.10; 'output': 0.13; 'result.': 0.15; '2016': 0.16; 'add,': 0.16; 'add.': 0.16; 'calculator': 0.16; 'clauses': 0.16; 'is",': 0.16; 'places.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'str()': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'example.': 0.18; 'input': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'commands,': 0.22; 'questions:': 0.22; 'cc:no real name:2**0': 0.22; 'code.': 0.23; 'this:': 0.23; 'words': 0.24; 'header:In- Reply-To:1': 0.24; 'example': 0.26; 'appear': 0.26; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; 'skip:u 20': 0.28; 'asks': 0.29; 'it\xe2\x80\x99s': 0.29; 'print': 0.30; 'code': 0.30; 'option': 0.31; 'another': 0.32; 'displayed': 0.33; 'quotes': 0.33; 'case,': 0.34; 'structure': 0.34; 'this?': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'according': 0.36; 'lines': 0.36; '(and': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'mean': 0.38; 'end': 0.39; 'why': 0.39; 'does': 0.39; 'your': 0.60; 'skip:u 10': 0.61; 'avoid': 0.61; 'times': 0.63; 'reuse': 0.66; 'here': 0.66; 'choose': 0.68; 'elizabeth': 0.72; 'divide': 0.84; 'now)': 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=F2BCzIciKERIlimY59zYIcdaFSIack0U08LQyuBZYRQ=; b=FWaEsIBDFjZTmH8xrWeiKSwZW52++CQbkpl4e9v6xDCPjG1exuegFXt9jFnpAkPqRZ OQy5B/D4mQgueSOUAizxkEmHT9Pf7OVr02dA5cOd8vml9X2e3s6+pbzqNopgW0+M7P8v avfcLudpp+XNJfDMIDYyXPw0mayr3932yJaI667PSoWSX7crB8VcbE1g3QKgtw7J+g4P qVqjNT+GnEqrYT/jvhH1xv4na3246SAa8F4gauTKfbRz4FOmkjfunHJNCkfjFVEhnSPW /JkS0AxWNqgEdUBP9M91p3uAVGMQkRcJ14US2bZfDm81qe0vPiSCfw6CpwWdn40VqI59 oJYg== |
| 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=F2BCzIciKERIlimY59zYIcdaFSIack0U08LQyuBZYRQ=; b=YmNradKC1i5UDJ2mk6PqcvvDYfuUnLAwcrkQaItyQLPSqXy5hJA2Tt8X9ED3KDCwVe IpJPu7nXZNe19I1Gra/ae8chqbE1dwJH47/5Vq0otceAKpSf6vEv0GiY3IcGlM27EIZQ GfBEMQd5nfR9ECOspOdA++OJxoo7BZ1x57HElCtQt+cBASnBPRzVrBp/RXrrI1vFZ7AU kG9DniNdF1DNCMt0a9lFSRGVTL/T0Q7/SQ8b1ItllLh8mUlYTVNY+oGYaL20sJsgTdfA en9TlozlmQK7IQtGKG9UbPHSrAIsyQdyRlq2OF6PMJIIUGJ/iDmappRXj4RyYTPJgPLZ qHMQ== |
| X-Gm-Message-State | ALyK8tLhWagDm9C9LvKcZ2McedVJb3u8NWpNs2sS8HQH732F5geR98bi/hspdLzF3ulrZ2TxDKLX9DKiDbsP9w== |
| X-Received | by 10.28.169.66 with SMTP id s63mr32366936wme.87.1467363432533; Fri, 01 Jul 2016 01:57:12 -0700 (PDT) |
| 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 | <CAMw+j7J5916Pz2TuYU9d+N3Eg+CuB16iERZA-+6x6th71sQ4LQ@mail.gmail.com> |
| X-Mailman-Original-References | <a0ed0591-dcbb-4ad2-b69a-58feef50153a@googlegroups.com> |
| Xref | csiph.com comp.lang.python:110889 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous 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