Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

[TUT]How to write a calculator in Python 2.6[TUT]

Hey HackForums and Python coders!

I'm Jedz and I'm gonna teach you how to write a calculator in Python. This is aimed mostly towards beginners, but anybody can read and benefit from it. :)
Anyway lets get on with it.

Any text in THIS COLOR is a new function that you will be using.

Okay so lets get started. First we need to define our calculator. We do this using this code:
def Calc():

What this allows us to do is type whatever you named your calculator (I named mine Calc) and if you type in
Code
Calc()
in your IDE it will run. This way is easier so you dont have to go into your files and run it.

Now we need our functions. And on a sidenote, I will put the whole calculator, finished and all, at the bottom as a reference. This is easy, we just print the functions and what number they correspond to in order for the user to access its functions.

Code
print "Please Select a function"
print "=="

print "1 Addition"

print "2 Subtraction"
print "3 Multiplication"
print "4 Division"
print "5 Exponent"
print "6 Square Root"
print "7 Area Rectangle"
print "8 Area Circle"
print "9 Circumference"
print "10 Absolute Value"

AWESOME! WE GOT OUR FUNCTIONS! Now you'll be asking how do we actually use input to get the functions to actually do their job. You got it.
We use input

So here is the code for our input function = input("> ")

Okay so what this does is allow the user to enter a number and it will actually do the task we wanted. OH MY GOD THATS AWESOME. Not really.

OK. We got out user input down. So what about the actual math part? Well before we can get to that we have to use the functions:
if, elif, else

What these functions do is if a user inputs X, then X will happen. if the use enters something else, then X will happen. elif is used when there are more than two choices.

First I want to distinguish between something. It's between the = and the ==. The single equal sign is used when SETTING A VALUE TO SOMETHING. The double equal is used when saying something IS TRUE TO SOMETHING ELSE.

Now that we have that down, lets get on to our conditional statements and computing the problems. We have a situation in which we want addition. To do this section it would be:
Code
if function==1:
    addend1 = input("Please enter addend ")
    addend2 = input("Please enter addend ")
    sum = addend1+addend2
    print "Your function has been completed"
    print "Sum is", sum
    raw_input("Press<enter>")

If you understand this you will be able to use this format with every function except for a few that I'll name in a bit. Just substitute the variables and set the values for whatever you like :)

Now as for those special functions, you will need to import a module. This module is called math. This module includes all the functions other than basic add, subtract,multiply, divide, and exponents(BTW, the syntax for exponents is the double asterik . So 52 = 25.). So you would need to use
Code
import math
for Absolute Value and Square roots. You can import the module and use
Code
dir(math)
to get a list of the functions in the math module. And to get a list of what one does you could do
Code
help(math)
.

Now, in order to use these you would do
Code
math.sqrt(x)
.
***IN ORDER TO USE AN IMPORTED MATH MODULE YOU MUST INCLUDE "math" FOLLOWED BY A PERIOD AND WHATEVER FUNCTION IN THE MODULE YOU WANT TO USE***

So once you figured everything out, your final product should be someone similar to mine:

Code
def FF():

    print "Welcome to Chris' ten function calulator program!"
    print "--"
    print

    print "Please Select a function"
    print "=="

    print "1 Addition"
    print "2 Subtraction"
    print "3 Multiplication"
    print "4 Division"
    print "5 Exponent"
    print "6 Square Root"
    print "7 Area Rectangle"
    print "8 Area Circle"
    print "9 Circumference"
    print "10 Absolute Value"

    function = input("> ")

    if function==1:
  addend1 = input("Please enter addend ")
  addend2 = input("Please enter addend ")
  sum = addend1+addend2
  print "Your function has been completed"
  print "Sum is", sum
  raw_input("Press<enter>")
    elif function==2:
  minuend = input("Please enter minuend ")
  subtrahend = input("Please enter subtrahend ")
  difference = minuend-subtrahend
  print "Your function has been completed"
  print "The difference is", difference
  raw_input("Press<enter>")
    elif function==3:
  multiplicand = input("Please enter multiplicand ")
  multiplier = input("Please enter multiplier ")
  product = multiplicand*multiplier
  print "Your function has been completed"
  print "The product is", product
  raw_input("Press<enter>")
    elif function==4:
  dividend = input("Please enter dividend ")
  divisor = input("Please enter divisor ")
  quotient = dividend/divisor
  print "Your function has been completed"
  print "The quotient is", quotient
  raw_input("Press<enter>")
    elif function==5:
  Base = input("Please enter base ")
  Power = input("Please enter power ")
  Answer = Base**Power
  print "Your function has been completed"
  print "The answer is", Answer
  raw_input("Press<enter>")
    elif function==6:
  import math
  RadicalTerm = input("Please enter radical term ")
  Answer = math.sqrt(RadicalTerm)
  print "Your function has been completed"
  print "The answer is", Answer
  raw_input("Press<enter>")
    elif function==7:
  width = input("Please enter width ")
  height = input(" Please enter height ")
  AreaRectangle = width*height
  print "Your function has been completed"
  print "The Area of the Rectangle is", AreaRectangle
  raw_input("Press<enter>")
    elif function==8:
  radius = input("Please enter radius ")
  AreaCircle = 3.14*(radius**2)
  print "Your function has been completed"
  print "The area of the circle is", AreaCircle
  raw_input("Press<enter>")
    elif function==9:
  pi = 3.14
  diameter = input("Please enter diameter ")
  Circumference = pi*diameter
  print "Your function has been completed"
  print "The circumference of the circle is", Circumference
  raw_input("Press<enter>")
    elif function==10:
  Value = input("Please enter value ")
  Answer = abs(Value)
  print "Your function has been completed"
  print "The Absolute Value of", Value, "is", Answer
  raw_input("Press<enter>")
    else:
  print "You have failed."
  raw_input("Press<enter>")

For some reason, the indents are messed up on my code. The standard indent for IDLE is 4 spaces. Keep in mind.
I you understood and enjoyed my tutorial :)

I'll be back for more. Please comment!

Jedz
Very nice Jedz! Keep up the great TUT's.
Thanks Paranoia! I may write one on wxPython soon if enough people ask me :)
I've had some code stolen before and I know how it feels. It sucks. So was my code or my instruction "stolen"?
Okay. I know I didnt steal it and thats all that matters. And its true.
No problem Buzz. Glad I could help :D
Haha i tried to make it easy to understand. This was my first 'real' program that actually served a purpose. I'm gonna write a wxPython tut maybe later tonight or tomorrow. Does any one have any tut requests?
thnx man helped quite abit
Very very nice tutorial thank you bro^^
This is really a great noob friendly tut


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 630 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by mantuoluoJun 14, 2021 02:35 PM 2 210 Jun 14, 2021 02:35 PM
Last Post: AeroBlue
[Python] by chaozuJan 22, 2020 04:30 AM 10 373 Jan 22, 2020 04:30 AM
Last Post: vorticalbox