Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #110878

Re: Creating a calculator

From Michael Torrie <torriem@gmail.com>
Newsgroups comp.lang.python
Subject Re: Creating a calculator
Date 2016-06-30 21:38 -0600
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>

Show all headers | 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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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