## Sunday, September 6, 2015

### Learn Quickly Creating Professional Looking Desktop Application Using Python - Part 3 of 7

Developing the console program >> Part 1

Quick links to other parts of this blog series:
Part-1 | Part-2 | Part-3 | Part-4 | Part-5 | Part-6 | Part-7

Welcome back! Hope u still remember the mission app we are going to build throughout this tutorial? If you don’t, kindly refer back to our introductory class where I mentioned that ...the app is going to be a mini calculator that can perform valid Mathematical Expression Operations of addition, subtraction, multiplication, and division.
Ok at this point, I wish to name the application "Expression Evaluator". Hope the name of our app is cool? Be free to suggest a name if you have something better in mind.

Today, I will explain how our app (Expression Evaluator) works on a command prompt (knowledge of Python basics will help). We will take advantage of on a powerful built-in python function used to evaluate mathematical expressions – the eval() function.

The completed code is attached below (image-6), if you understood every bit of it (the code), then go sleep and wait for the next class . But if you are struggling to read/understand/interpret the code, kindly be patient and follow along as I will explain every single line of the code soon.

~~ Now head over to your desktop or any location on your PC and create a project folder (this where we will save all our files) name it “EXPRESSION_EVALUATOR” then save a python file inside it from your text editor as “eval.py”. See image-7 below.

~~ Type the attached code (image-6) in your “eval.py” file and save it.

~~ If you run/launch the “eval.py” file by double clicking on it, you should see a command prompt as on image-8 asking you to enter an expression.

~~ Type a valid mathematical expression and press enter to see the result. If you entered an invalid expression, the program will out an error massage.
As shown on image-9 I tried expressing “2+2”, and it gave me “4” excellent!
When I tried expressing “2+a”, the console crashed after showing an error message - try it! This is because “a” isn’t a number and isn’t defined, making the expression invalid (hope you still remember your maths class!).

~~ A better way in my opinion of running the “eval.py” file is by opening the project folder then the file through a command prompt, this way your cmd prompt window will remain open no matter what happened and you can see all errors. Here is what I mean;-
From within the project folder, hold “Shift” button on your keyboard and right-click inside the folder then select “Open command window here” – see image-10. This will open the command prompt directly from the folder.

~~ Type python space then name of the file (that is: python eval.py) and hit enter. You should see a screen similar to the previous one (except for the folder’s path on the first line) asking you to enter an expression. See image-11.

~~ This time even if we entered an invalid expression our program with never close automatically until we decide to close it manually. Or better still uncomment line-14 in “eval.py” file so however we ran our file it will never close until we press enter.

Ok, let’s dive into detail explanation of each line of code in “eval.py” file in the next class.
Till then digest today’s lesson and have a nice day!

Warning: Just a word of warning, the python “eval()” function can be dangerous if you do not know what you are doing (it can be use to delete system files). So in the meantime just use it for simple examples such as the ones shown here.

Developing the console program >> Part 2

Today, let me quickly explain every line of our “eval.py” code file (as seen attached below).
Basically there are less than 15 lines of codes that made-up the console version of the Expression Evaluator app! Starting from;-

Line-1: This line is called “SheBang” or “HashBang”. It tells python shell;-
- That the file is a script
- To use python to execute the script
- The path to the python interpreter.

Line-2: Blank

Line-3: Here we have a print() function that outputs a string onto the console. In this case, the string within the single quote will be written on the console.

Line-4: The same as line-3 above. But here we are telling python to write out the hyphen “-” character 40 times, to create a ruled line beneath the above string. This is mainly for decoration.

Line-5: Blank

Line-6: This is used to handle errors, try...except statement block starts here. We basically put our usual statements within the try-block and put all our error handlers in the except-block.

Line-7: Here the raw_input() function is use to get user input on the command prompt and stored in a variable called “exp” for further processing. This is done within the try...except block, so note the indentation.

Line-8: We then pass the variable “exp” into the eval() function. Then store the result in a new variable called “Answer”.

Line-9: Blank

Line-10: The two variables “exp” and “Answer” are printed on the console with the aid of string formatting function.

Line-11: raw_input() function is used to allow the user to terminate successful expression evaluation.

Line-12: Error handlers are placed in the except-block.

Line-13: In this case the error handler in the except-block is to use print function to outputs the string 'Ooops! ...'.

Line-14: This is optional, if the “#” sign is removed, raw_input() function will allow the user to terminate an unsuccessful expression evaluation.

Line-15: Blank

That is it! Our next task is to convert this "eval.py" code (as seen attached below) into a more user friendly interface application for windows PC, where the user uses elements such as button, text input box to manipulate the app.

Peace!