Super Simple Calculator

Back to index

TEXT EDITORS:

If you are using a Macintosh computer chances are that you are using the pico text editor. Other popular text editors include pi, joe, and edit. Most work more or less the same. Some are syntax sensitive and will actually recognize Python keywords and display them in different colors. This is a nice feature since it helps you during the debugging process. No one is perfect and so there are always mistakes to fix. The process of fixing mistakes is called debugging.

To kick things off. Get the following sample program up and running:

#!/usr/bin/python print "Super Simple Calculator" num1 = raw_input("Enter integer value: ") num2 = raw_input("Enter another integer value: ") op = raw_input("Enter p for plus and m for minus: ") print "Your input: " + num1 + ", " + num2 + ", " + op if op=="p": answer = num1 + num2 print "ANSWER: " + answer elif op=="m": answer = num1 - num2 print "ANSWER: " + answer else: print "There is a problem with your input."
One of the peculiarities of Python is that it is picky about indentation. You must indent consistently and correctly in order to get the program to function as intended. In this example you see something called an if-elif-else construct. As you might guess, elif is short for else if. Basically, the program checks first to see if op="p" and if it does then it executes the next two lines and the program is done. If op does not equal "p", then the program checks to see if op equals "m" and if it does then it executes the next two lines and the program is done. Otherwise, the program prints out "There is a problem with your input." and then it is done. Although you can have only one if and only one else in this construct, you can have as many elif lines as you want and this gets us to the assigment! (BTW, an if-else construct can work without any elif statements and an if statement can function without an else statement.)
ASSIGNMENT:

You will extend the sample program so that it performs multiplication and division. Also instead of having single letter commands, you will process input for commands which are at least three letters long.