We’ll be creating a little calculator script in Python using the console. You’ll see how to use the while loop, how to convert strings to ints and all sorts of wizardry the likes of which the world has never seen the likes of which.
Creating our helper functions.
First let’s create a little method that we’ll use to convert strings. It’s simple: If you can convert to int, convert it, if you cannot, convert it to a float.
Here what it looks like:
def convertString(str):
try:
returnValue = int(str)
except ValueError:
returnValue = float(str)
return returnValue
Now let’s define the four little methods that account for the basic arithmetic our application will do:
def addition(a, B)/>:
return convertString(a) + convertString(B)/>
def subtraction(a, B)/>:
return convertString(a) - convertString(B)/>
def multiplication(a, B)/>:
return convertString(a) * convertString(B)/>
def division(a, B)/>:
return convertString(a) / convertString(B)/>
Notice we’re calling our helper method, convertString; and not the builtin Python method ‘int()’.
Looping keeps things running
We’ll declare a little boolean flag variable to let us know if we should keep running this application or not. We’ll call it keepProgramRunning.
keepProgramRunning = True
print "Welcome to the Calculator!"
while keepProgramRunning:
#Whatever is in here will run until the end of time
#or until the variable is false. Whichever comes first.
Let’s create a little console menu so our users can choose what to do.
keepProgramRunning = True
print "Welcome to the Calculator!"
while keepProgramRunning:
print "Please choose what you'd like to do:"
print "0: Addition"
print "1: Subtraction"
print "2: Multiplication"
print "3: Division"
print "4: Quit Application"
#Capture the menu choice.
choice = raw_input()
if choice == "0":
#Something goes here.
elif choice == "1":
#Ditto.
elif choice == "2":
#Another option
elif choice == "3":
#What?
elif choice == "4":
print "Bye!"
keepProgramRunning = False
else:
print "Please choose a valid option."
print "\n"
Let’s break this down, we’re capturing the input and then using if statements to determine where we’re going. If the if statements don’t find a suitable response, you just tell the user to stop being so dumb and choose something that would work.
Remember, since this is all inside of the while loop and the keeProgramRunning variable hasn’t been touched, what will happen when it reaches the end of the script? That’s right! It’ll display the menu again and prompt the user for input.
Only when a user chooses ’4′ will the program end, and that’s because we’re changing the keepProgramRunning variable to False, which let’s us break free from the constricting While loop.
Now let’s fill in the if statements and give them something to do:
def convertString(str):
try:
returnValue = int(str)
except ValueError:
returnValue = float(str)
return returnValue
def addition(a, B)/>:
return convertString(a) + convertString(B)/>
def subtraction(a, B)/>:
return convertString(a) - convertString(B)/>
def multiplication(a, B)/>:
return convertString(a) * convertString(B)/>
def division(a, B)/>:
return convertString(a) / convertString(B)/>
keepProgramRunning = True
print "Welcome to the Calculator!"
while keepProgramRunning:
print "Please choose what you'd like to do:"
print "0: Addition"
print "1: Subtraction"
print "2: Multiplication"
print "3: Division"
print "4: Quit Application"
#Capture the menu choice.
choice = raw_input()
if choice == "0":
numberA = raw_input("Enter your first number: ")
numberB = raw_input("Enter your second number: ")
print "Your result is:"
print addition(numberA, numberB)
elif choice == "1":
numberA = raw_input("Enter your first number: ")
numberB = raw_input("Enter your second number: ")
print "Your result is:"
print subtraction(numberA, numberB)
elif choice == "2":
numberA = raw_input("Enter your first number: ")
numberB = raw_input("Enter your second number: ")
print "Your result is:"
print multiplication(numberA, numberB)
elif choice == "3":
numberA = raw_input("Enter your first number: ")
numberB = raw_input("Enter your second number: ")
print "Your result is:"
print division(numberA, numberB)
elif choice == "4":
print "Bye!"
keepProgramRunning = False
else:
print "Please choose a valid option."
print "\n"
We capture the input, save it to a variable, give the two variables to our method and voila – you’re very first calculator in Python.
Hope you liked this brief tutorial.
------------------------------------------------------
If you liked this tutorial, you can read more articles written by me at my blog. Visit AlphaOT




MultiQuote





|