BAYMAX: My First Python Project

by syfrog in Circuits > Software

11227 Views, 26 Favorites, 0 Comments

BAYMAX: My First Python Project

bymx13.JPG
bymx2.JPG

For several years (ok maybe a decade) I've had this on-off relationship with learning to code. I've dabbled in this and that picking up some Java script here, HTML there, snips of VBA and working knowledge of all manor of industrial logic controls. This has been fun but has not fully scratched my coding itch, that being to learn to be proficient at C++ and C#.

Part of my struggle that I mention in my Arduino shield programming 'able is that at my core I am a mechanic, a hardware guy. If I can't find a useful and/or practical outlet for this 'virtual' knowledge it just does not take. Sure I can muddle through all the tutorials and get my computer screen to print 'Hello World' but its just not fun or that is to say its not practically usable knowledge that I care to retain.

Luckily in hearing my quandary someone introduced me to Python as an 'entry level' coding drug. Being low on syntax and having a wealth of info available I was able to feel productive at entry level and see instant possibilities. I was hooked and within a week I'd moved from 'Hello World' to in the throws of developing my own command line interface and now trying to master file creation and manipulation.

Scope:

In this instructable I'll share what I learned about Python3 while developing my first Python project. This project started out as a sort of computer furby/ tamagotchi pet. But this quickly evolved into a command line interface I code named BAYMAX (after the character in the Bighero6 movie.) So....

LETS BUILD BAYMAX!!

Step 1: Get Python

bymx1.JPG
bymx5.JPG

To start programming Python you are going to have to download Python3.

I am not going to step through how to download the program this is well documented on the interwebs.

But I will touch on a few questions that I had to sift through when I got started with Python that are not very clear cut for the novice beginner.

Python Getting started FAQs: (for the beginner)

Q1. I hear about all these python coding tools Pycharm etc.. Do I have to download Python AND a development program to actually write Python code??

A1. NO. Keep it simple to start. Just download Python from www.python.org

There are other 'fancier' development/supplemental coding software out there; as a beginner you do not need them. IDLE is the name of the 'stock' development environment (i.e. place where you write your Python code) IDLE comes in the download package from Python.org. I've had no problems with IDLE it is simple, it does the job.

_________________________________

Q2. For programming python Should I download the older version 2.x.x / Python2 or the newer version 3.x.x / Python3?

A2. Download Python3. The current version at time of this instruction is v3.5.1

You are getting started now programming python. Python3 is the future of this language. Developers may need Python 2 for old legacy projects. You have no concern with this. You just want to get into computer programming. Don't sweat the details at this point.

***NOTE on installing on newer Windows 10 machines***

Beware of the default download location on newer windows machines.

Be sure to install Python as an ADMIN install / available to ALL USERS when prompted.

If you install Python through the default settings newer Windows will install Python as an 'App' in the User/downloads/app/etc/etc/..... basically Python will not get put in the main program86/bla. bla. location and you may lose functionality with your Python codes later down the road.

I ran into an issue myself on my first install of Python.

The Python package installed fine and my python code seemed to run fine inside IDLE but. I ran into issues trying to control things outside of IDLE.My codes would not open other programs etc.. A reinstall of Python on the machine 'Available for all users' did the trick.

Step 2: Make a Plan

aa_ideas.jpe

After you have Python installed take a few minutes to get familiar with the IDLE interface to begin programming python. Get familiar with the basic python syntax and basic uses of a few Python programming commands namely; print(), lists(), dictionaries() and if,else,while logic structures.

I my research the web locations that I found the most helpful for the beginner programming python were tutorialspoint , the python3 documentation and stackoverflow.

.....All that out of the way Now its time to get serious (well not too serious we are building Baymax ),

Now lets make a plan and do it...

Quick evolution of my project plan:

While I was tinkering with Python programming in the beginning I hit all the tutorial sites mentioned above. I ended up with a few nice bits of 'novelty' code. But I was self aware of my past coding attention issues and failures soo... I wanted a new approach. I needed to plan i needed a clear goal to achieve.. I needed something I could build that was useful. So I set out to answer the following question:

What type of project for personal use could I start as a novice and build on as my coding skill increases. What I settled on was to build a python program that I can use to command all of my small snippets of python example code that I wrote on my way to becoming awesome--er. Then it dawned on me this sounds a lot like a command line interface. As I googled Python + command line interface I learned that such a thing was not outside of the realm of skills of a beginning python programmer if the structure was simple to begin with.

So start working the problem......

TIP: Don't think about the solution yet, focus on the need or the question:

I started this python program project just like I was writing a short story. First I started by writing out in plain ole' English sentences what I wanted my python program to do. . I did a few flow chart sketches for giggles to help me visualize the flow. Next I started breaking the idea into an index style small steps.

Once each thought was broken into a step.

Then comes the Python programming to figure out how to make these steps work.

** I feel it bears repeating that in the start DO NOT get lost in having 'Python programming' solution or worrying about how. To start you only need a direction, questions to answer a goal***

For my Baymax Python Programming Project I wanted to:

--- Print lines to the screen to simulate an operating system booting up, (Think Old DOS computer)

--- random OS boot screen stuff

--- maybe a text picture of baymax

--- greetings screen where baymax says hello

--- Ask for a login: user name and password

--- Will accept any user name

--- stored password must be entered correctly

--- if password is correct you can continue to command promp.

--- if password is incorrect you are asked again

--- Open command prompt

--- DOS style command prompt

--- prints a hint reminding the user to type help for list of availble commands

Brainstorming..............

--- What to command from this interface??

--- open web browser to google something??

--- create a .txt file to enter notes??

In this brain storming I fleshed out what I really wanted to create.

The next step was to simplify this 'sketch' above into more refined clearly defined steps.

Step 3: Idea to Index

bymx6.JPG

The Next in my steps is simplify the python Baymax program into an index style;

Starting now to consider how a computer 'thinks' If you can explain in 1 simple sentence the action you want this is a great start to building a function list. If you struggle to fit a step idea into 1 sentence break it up into multiple sentences. To some this approach may seen like a waste of time. But putting in this thought work upfront will make 'translating' your ideas into code easier.

Referring to the pic above the python functions I'm trying to create could fall into the following 3:

bootupbaymax() << Print all the 'DOS' Simulation stuff

logon() << Ask for a user name and password to unlock the command promp

cmdpromp() << Launch a command line interface prompt that can do....Stuff....

Combining my brainstorm and the index / simple function list to get a more visual idea of the python program organization. From this overview 'map' I jumped into coding the first function.

Step 4: Write It Out...

Since the first function is mostly print to screen stuff. Using a text editor I wrote out with close to the proper white spacing what I wanted the boot screen simulation to look like when Baymaxs' bootup was complete

_______________________________________________

Starting to build BAY-MAX OS_1.0.......

HEROmem is testing extended memory....

done....

Virtual Machine Folder Sharing Copyright (C) 2016 Syfrog Corp. All Rights Reserved

(o-o)

I AM BAYMAX

--------------------------------------------------------------

The next step is to translate my English text into a Python Program.....

Step 5: Coding the Bootup Screen

bymx8.JPG
bymx9.JPG

Having done all the foundation work to this point the python programming code is easy peezy**

In the next coding steps please Refer to the pics above the step for my Python Programming Code with notes for the best description of what is going on.

NOTE: For each section I created a new file in python and saved each function separately to make debugging and testing easier to manage.

______________

For the bootupbaymax() function I basically only had to cut and paste my text file sketch and add the print function syntax--> print('what you want to say here...') <----

Baymax lives!!!

In running this first function of my programming Baymax Python Program the first time I recall mixed emotions.

Of coarse I was excited that this first step worked without crashing. But I also faced discontentment,

I could do better.

The code ran but because all this print() code displays nearly at the same time to the screen there is no simulation that anything is happening. There needed to be some pauses.

At this point my little code was ... boring.. This is great though because necessity is the mother of invention.

At this point I had my first "OK how do I learn how to do this now..." moment.

The next skill I needed to learn was How can print commands be paused??

Python programming does not disappoint as this either it was super easy just import time to the script and add sleep commands as seen in the next step.

Step 6: Boot Screen Upgrade

bymx10.JPG
bymx11.JPG
bymx12.JPG

I searched the interwebs for python program examples and learned that the Python time module could be used to give me the pauses I was now so desperately looking for so my Baymax would start to feel more alive.

New to this Question: What is a module in Python??

A python program module is coding jargon for basically a python file that is being referenced, for example you could write a single function in a script and save it as myfunction.py later you can use that function simply by putting import myfunction at the top of your new program file. Your myfunction.py file is being used as a module.

Python time module

I'm not going to bore you with all the functionality of the time module a few useful ones are that will allow you to display current time and print a monthly calendar to the screen.

The pics above show the change to the boot up screen with the pauses added. To show the evolution of the boot screen the last pic in this step shows this function in its final/(current for now) state.

Baymax "os system" is booting up now.

The next thing on my Baymax Program to do was to have a simple log in function to allow or deny access to the command input interface.

Step 7: Logon()

logon1.JPG
badcode.JPG
logon1run.JPG

Epic Fail #1

To code the logon-on function logon() we move away from the print() command and have to start learning how to use basic logic / decision functions in Python programming.

Referring back to my original thoughts on what I wanted this Baymax program to do:

--- Ask for a login: user name and password
--- Will accept any user name

--- stored password must be entered correctly

--- if password is correct you can continue to command promp.

--- if password is incorrect you are asked again

input() command is how we accept user input in Python.

The way the input command syntax works is that whatever you put in the parenthesis after input is what is displayed next to the entry prompt for the user.

To begin coding the logon I opened a new python file in IDLE and saved
it as logon.py Building small snippets of code separately and combining them later is easier to juggle and troubleshoot.

Check the notes in the screen shots above for details and see how the input is taken and used.

The python example code in the above pics was my first attempt at a logon input.

Pic 2 Note how the If and else functions work.

I added the final print line after the if, else, to monitor if the python program was functioning as I want.

It clearly is not exactly what I intended as the last line get printed if the correct password is entered or not.

As far as pure function of the code what I want is for this piece of code to ask for the correct password an undetermined number of times until the correct password is entered. I need a loop. The while and do while commands do just what I am looking for.

The next Step will show a python program example of adding a while command into this function.

Step 8: Python Programming Using While Command

while1.JPG
while2.JPG
while3.JPG
while4.JPG
while5.JPG

The while loop command in python programming works by using a True, False value check to determine when to stop executing the script below the while loop.

Basic syntax is:

while |something here is true| :

|do this stuff here|

while loops can be used to drive if, else and elif statements and the Boolean - true/false - check can be multiple conditions.

Follow the pics above. I feel examples of each one of failure steps will give a better understanding of using this loop then me trying to word it while not being an expert myself. Many python programming tutorials show you the correct way but offer not much insight by showing you ways to fail and clues to how to recover from the failure.

Step 9: Combining Bootup and Logon

logon2.JPG
bootnlog1.JPG
bootnlog2.JPG

The next step was to get the the logon script in the format of a function so it can be easily called and ran. As seen in the first pic above this is a simple task just name the function def logon(): then alter the white space indention for the lines under the name.

Now we have 2 functions that we are happy and working for now and 2/3 of your first python programming project is complete.

Simply cut and paste the logon function into the boot up script as seen above. and run them as 1 file..

See how simple this Python Programming thing is getting...

Now to the CLI (command line interface)

Step 10: Command Line Prompt

The last section of this Baymax Python project was to create a command line prompt that would allow the verified user to do ... Stuff.. The stuff you want to do could be anything. Open a windows program. Create a new text file. Open a web page.

As you can see also most of this stuff I just mentioned will take some time-in-seat coding to learn how to do.

For this reason my intent for this piece of the project was to build something expandable and useful as my coding skill level grows. Whatever you learn to do in Python from here out can be put in function format and then called at this single command prompt.

Next skill to acquire is?..........

How can a Python function be called directly??

I spent more time on this by far than on the other 2 functions so far (a few weeks researching simple command line interfaces as opposed to a few days learning syntax.)

Enter dictionaries--- a dictionary in Python is a list of values that has an associated 'key' Think of this as a value with a code-name (the key) you can only call the value or alter the value in the dictionary if you know the values 'key.'

So if you had a bunch of functions you could list them in a dictionary and call them using the dictionary keys.

Doing this also will allow us to restrict the user entering commands into our command line interface to only be able to call the functions that we offer in our dictionary of available commands/functions etc...

Step 11: Evolution of Cmdpromp Function

CLI_1.JPG
CLI_2.JPG
CLI_3.JPG
CLI_4.JPG
cli_5.JPG
cli_6.JPG

Open another new python file in IDLE.

Create 2 or 3 small functions and build a dictionary out of them. dictionaries use { } curly brackets the key and value are separated by a : colon and entries are separated with a , comma.

Follow the pics above to see the full syntax and an examples. The progression of the code is adding another while command in this case we have no break out because the command line prompt is the end of this script and should return to ask for more commands.

PIC 4: Note if you try the code in pic 4 as shown it will not run because the dictionary name func_dict does not match the existing dictionary available_function_dictionary.

Issues to resolve

As seen in the last picture this basic starter code will crash if the user enters something outside of the dictionary keys available. I am still working on my solution for this issue.

Python Function Examples

func_1.JPG
func_3.JPG
func_2.JPG

Now you are a python programmer but you need a kick-start. This step just showcases sample python programming functions that I use in my Baymax CLI function dictionary as available commands that 'Baymax' allows me to access from the command prompt.

The first pic shows small functions that call other windows programs to open things.

In the command prompt typing 'google' will open a new tab in the default web-browser to the google.com. Typing 'wordpad' and 'sketchup' opens a a new word pad file and google sketch up, respectively.

The 2nd pic above shows the 2 lists of available responses and greetings. The 3rd pic shows how I am accessing these list items randomly so the Baymax programing does not repeat the same things over and over, this way he can showcase his personality. :-)

Closing Thoughts

Clearly this python project is still developing so Whats next for developing this Baymax python program??

The next steps I'm working on:

- Entry error allowance so the interface will not crash when an unexpected input is put in. Handling exceptions with Python using the built in 'try' function.

- Exploring using web scrapping to report news and weather updates when you log-in

- Exploring saving users' names and profile info to build on later?

- What about a note pad Command that will 'record' typed entries from the command line to a text file that builds (instead of opening word pad and having to switch programs in the middle of work.

- Maybe go back and write that text adventure I first tossed around

Anyone interested in getting the full text version of my current work in progress python program example use the link below to my google drive share-all location.

Baymax Full Text Code

Hopefully I've shared enough of what I've figured out about Python Programming to help a few others be dangerous Python Programmers.

So what are you waiting on... Go Make something!!