Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Warrick Newsgroups: comp.lang.python Subject: Re: Creating a calculator Date: Fri, 1 Jul 2016 10:57:11 +0200 Lines: 74 Message-ID: References: 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: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:110889 On 1 July 2016 at 05:08, 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=3Dinput(":") > if user_input=3D=3D"quit": > break > elif user_input=3D=3D"add": > num1=3Dfloat(input("Enter a number")) > num2=3Dfloat(input("Enter another number")) > result=3Dstr(num1+num2) > print("The answer is"+ result) > elif user_input=3D=3D"subtract": > num1=3Dfloat(input("Enter a number")) > num2=3Dfloat(input("Enter another number")) > result=3Dstr(num1-num2) > print("The answer is"+result) > > Two questions: > 1. Why do I need to put ' ' around the words add, subtract, multiply, qui= t, etc. when it is already in quotes in print()? When the calculator asks m= e which option I would like to choose I do not write 'add'- I only write ad= d. 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 outpu= t line could be put outside the if statements to omit repetition of code. W= hat 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=3Dfloat(input("Enter a number")) > num2=3Dfloat(input("Enter another number")) > print("The answer is"+ result) This is code repetition. It=E2=80=99s 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 =3D=3D 'add': result =3D num1 + num2 # no need to call str() if you use commas in pr= int() elif user_input =3D=3D 'subtract': result =3D num1 - num2 # other elif clauses go here print("The result is", result) --=20 Chris Warrick PGP: 5EAAEA16