How to Create a Simple Calculator in Python

Submitted by: 
Language: 
Visitors have accessed this post 417 times.

Introduction:
This tutorial is on how to create a simple calculator in Python.

The Theory:
Here's how the program will be structured:
Get the users first number
Get the users second number
Get the users action
Use if statements to perform the calculation
Print out the result

Getting The Users Numbers:
First we want to get the users two numbers to calculate together somehow. We use input statements to allow the user to enter a string, we use an int cast around the input statement to convert their string input to a integer for us to use in our calculations...

  1. num1 = int(input("Enter your first number: "));
  2. num2 = int(input("Enter your second number: "));

This is good but the user could enter an invalid number, so lets add some exception handling...

  1. pass1 = -1;
  2. while (pass1 < 0):
  3. try:
  4. num1 = int(input("Enter your first number: "));
  5. pass1 = 10;
  6. except ValueError:
  7. pass1 = -1;
  8. print("That is not a valid number. It must be a whole number...");
  9. pass2 = -1;
  10. while (pass2 < 0):
  11. try:
  12. num2 = int(input("Enter your second number: "));
  13. pass2 = 10;
  14. except ValueError:
  15. pass2 = -1;
  16. print("That is not a valid number. It must be a whole number...");

So now the script creates two variables "pass1" and "pass" and sets them both to minus 1 (-1). Then, while the relevant pass variable is below 0, it asks for a nuumber. If no exception is thrown, the pass variable is set higher than 0 and the loop is passed, otherwise an error statement is printed and pass1 is re-set back below zero (this isn't necessary but it makes it simpler to understand).

Asking For The Action:
Next we want the action for the two numbers, this is similar to before...

  1. pass3 = -1;
  2. while (pass3 < 0):
  3. try:
  4. action = input("Enter your action (+/*-): ");
  5. if (action == "+" or action == "-" or action == "/" or action == "*"):
  6. pass3 = 10;
  7. except:
  8. pass3 = -1;
  9. print("That is not a valid action. It must be +, -, /, or *...");

Validating The Action and Performing The Calculation:
Finally we want to perform the calculation depending on what the action entered by the user was...

  1. if (action == "+"):
  2. end = num1 + num2;
  3. elif (action == "-"):
  4. end = num1 - num2;
  5. elif (action == "/"):
  6. end = num1 / num2;
  7. elif (action == "*"):
  8. end = num1 * num2;
  9. else:
  10. print("Error...");

Last but not least, we output the result...

  1. print("Your result is: " + str(end))

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.