A few weeks ago I discussed my emotional experience of my “first” coding project. Today I would like to share some actual code and the experience of my first coding “project” (mind the quotes).
Ever since I started coding at Flatiron, I had a very slight but persistent feeling of stress and anxiety, caused by the amount of new information being pumped into that brain of mine. Whenever I reach a new milestone in my learning process, I am greeted with another lab or project to push myself forward. And even when you seem to be done for today, the instructors drop another bombshell that is a couple weeks worth of tasks to work on. Don’t get me wrong, it is great to be constantly on the edge of your seat testing your mental capabilities and trying to achieve results other people would take years to achieve.
At the bootcamp we are constantly reminded that stress is where the growth happens. But it is nice to occasionally unwind, take your eyes off the challenge and have a look at your small victories — completed labs, projects, tasks and the amount of knowledge you gained while completing them. It is liberating to see how much your coding habits improved and have a giggle at the task your past self found hard and struggled to complete.
On one such occasion I was browsing through my repl.it account and came across my first attempts at coding. One of such poorly executed attempts was my first CLI app coded in Python. That is exactly what I want to share with you today.
My CLI app
The app that I coded was a text-based Python 2.7 game of Hangman without a lot of visuals, but it would tell the player what letters he had guessed, their position in a given word and how many attempts he had left. You can try it out yourself.
The user would first be greeted with the following passive-aggressive message:
After playing one “game” here is the result:
First of all, in case you are not aware, I must inform you that a tomato is, in fact, a fruit. Second, you might have noticed that the task is to guess a fruit and nothing else. Well, that is because I had to hard code all the possible words that the game would randomly choose for the player to guess. To say it was short would be a gross under-statement. My list was a whole 6 words to choose from. I made that decision to be able to easily troubleshoot the bugs and could not be bothered to improve on the list after I was finished.
The game would also finish with a victory in case you decided to enter the entire word as a guess.
The code behind the app
So how does this beast (more like monstrosity) function? Well, the principle is very simple. I will give a brief explanation of what happens in each block of code:
my_list
is an array of words, which the game randomly selects The word is then split into individual letters.
On line 9 Python magic stolen from Stackoverflow.
gg
welcomes the user and accepts the first letter.
Lines 13–17 check how many times the letter repeats itself in the word (if it does) and goes into a while
loop.
19–29 checks if the letter is present in a the word and if it is — show its position on the screen.
31–32 adds the last input to the list of guessed letters.
33–36 compares the split word and all the guesses and congratulates if the guess was correct.
37–43 checks if the letter was correct and request the user for further input.
44–47 checks for incorrect guess, reduces number of remaining guesses
48–49 the user has lost.
51 the user guessed the word.
Before posting the app I thought of changing the variables and make the code more readable, however that would’ve beaten the purpose of this post — showing curiosity and the first baby steps of my coding path.
Problems yet to be resolved
What you just saw was the perfect scenario in case the user was not a complete waste of DNA and knew how to follow instructions on screen. Therefore the game still has a multitude of bugs, some of which are:
- The game would accept an empty string as a guessing attempt
- The game would accept more than 1 letter as a failed attempt
- It would not accept a longer part of the word even if it was correct
- If the letter is in multiple places — player needs to input multiple times. (lines 38–43 tried to account for that, but does not work)
- Up to 3 recurring letters in a word
- See picture below
Also, on the word “maracuya” the game refused to end. Probably because that’s not an English word. That’s a 7, nonetheless.
… and a number of other problems I could not discover in the time of writing this blog.
Remarks
This 53 line abomination is a valuable part of my coding journey. It shows how lack of experience and a passion for code collide. It took me over 30 hours to get the code running with the amount of knowledge I possessed after 15 hours of prior coding experience. It may seem trivial or even useless, however it was when this project was completed, when I started thinking about code seriously and that ultimately pushed me to join the bootcamp, which is why I hold this piece of code so dear. Additionally, it was nice to remind myself of the way of the Python.
I might get back to the project in the future and make a more stable and versatile version of it, that would account for all (or at least part) of the above mentioned problems. I would likely recreate the project in ruby as I am by a long shot more proficient and confident building a CLI in the language. I would also make variables and functions more readable and more self-explanatory both for myself and for that occasional poor soul that decides to read through the mess that is the current version of the program.
This is a reminder that “In order to reach perfection one must begin by being ignorant of a great deal.” (F. Dostoevsky)