Wednesday, March 10, 2021

Project 4: 3 times the... GAME OVER

Welcome back to my blog for this month's post!  Somehow, I've just realized that I didn't need to rewrite the post titles each time!  πŸ˜…  Previews are a lot more valuable than I initially thought...  Anyway, this is my third and FINAL post in what ended up becoming my Guess the Number series.  If you would like a re-cap for my previous Guess the Number posts, here is Project 2 and Project 3.  

As you may have guessed from the title, I will talk about how I implemented a "game over" after a limited number of guesses.  My previous posts introduced codes that would allow users to make guesses as to what number the program has chosen, but the user was given an unlimited number of guesses.  Many people who have ever played games would know that a game with no changes in difficulty, limitations or consequences can become quite dull after a few playthroughs.  In this final instalment of Guess the Number, I will introduce two codes that give the user three guesses, and if the user gets them all wrong, the game will declare a game over.  

I won't be introducing any new types of functions and operators this time, but rather use nested if statements and add extra lines defining the conditions.  There are two versions of the updated game.  In both versions, if a number below 1 or above 10 was entered, the response will be an error message suggesting that they have guessed a number outside of the range.  However, in the first version the user will lose a guess if they type in a number outside of the range and in the second version they will not.  I decided to write about both versions of the game in this post because I found it interesting that despite the first version arguably being the harder game, it was actually the easier code to plan for.  

First version code:  You can view the code below by either downloading the PDF from the link or by looking at the image posted here.

Code PDF:  Loss of chances 


Here's a list of statements that I've added to implement a game over:

  • c=3
    • Allows Python to define c as 3
    • The user has three chances to guess the correct number 
  • c=c-1
    • Every time the loop is repeated, the new c is minus 1 of the old c
    • The user originally has three chances.  If they get one guess wrong, they lose one chance and is left with two chances.  If they get another guess wrong, they lose another chance and is left with one chance...
  • if c>0
    • As long as the user hasn't run out of chances, they will receive a statement regarding their input (and the number of chances they have left)
  • else
    • In this case, if c is 0 or smaller
    • When the user runs out of guesses, they will receive a "GAME OVER"
First version playthroughs:  You can view the outcome of the playthrough by downloading the PDF from the link or by looking at the image here.



















Second version code:  You can view the code below by either downloading the PDF from the link or by looking at the image posted here.

Code PDF:  No loss of chances 

 
The additional statement:
  • c=c+1
    • Before the loop c=3
    • During the first loop c=2 because c=c-1
    • But when the user types in a number lower than 1 and above 10, c=3 again because c=(c-1)+1
Second version playthrough:  You can view the outcome of the playthrough by downloading the PDF from the link or by looking at the image here.




















See how adding a few simple equations can build a GAME OVER?  Before I started coding, I never really knew how games seemed to figure out how I made the wrong move, but at least now I see that it's more-or-less a matter of getting the program to recognize specific patterns as either "wrong" or "right."  I thoroughly enjoyed making Guess the Number minigames and I hope you enjoyed reading about my journey!  However, I would like to move on to other projects to diversify my coding skills, so stay tuned for future posts πŸ˜™

As always, let me know what you thought of my codes πŸ˜€  Do you have any better ideas as to how to make Guess the Number?  Leave comments below or if you prefer to contact me personally, check out the Contact Me page.  πŸ‘

The next post will NOT be a project but will most likely be a storytime πŸ˜‹  Nevertheless, it is something that I have planned to write about for some time now and I hope it will be an interesting read πŸ˜‰


Tuesday, February 9, 2021

Project 3: Old vs New

 Project 3:  Old vs New

Hello!  Welcome to my new project, which focuses on making a new Guess the Number code.  It's the same program but the code is shorter!  Makes coding life a lot easier if you have fewer lines to type.  

As a recap, have a look at Project 2!  Below is the original code for Guess the Number:

If you prefer to view the old Guess the Number code in PDF format, click here!


In this post, I would like to introduce "or":

"or" is an operator that means "if A or B apply, execute X".  Have you noticed that there are some conditions that lead to the same outcome in the original code?  That is a valid way of making code, but if you have to make a code that has hundreds or thousands of lines, this can not only feel repetitive and annoying, but it can also make it harder to review your code for mistakes...  This is when operators like "or" can be useful, to reduce this repetitiveness as much as possible.  

The code below is the new version of the Guess the Number code where "or" is used for conditions that lead to the same outcome:


You may, or may not, have noticed another difference between the old and new codes.  The new code also has a function inside another function for y.  This is called a "nested function".  If you simply want the user to be prompted to enter a number, and want that same number to be recognized as a number by python, the nested function allows both the prompting and recognition to be done in one line instead of keeping the commands in separate lines.  

The new code includes "or" and a nested function.  This allowed the Guess the Number code to be shortened from 36 lines to 16 lines!  That's over half the number of lines gone!!

BUT!!!  The most important question:  Was it worth shortening the code?  Did it affect the quality of the program itself?  

The answer:  NOTHING TO WORRY ABOUT 😌😎  

In this case, the program ended up being pretty much the same as the original code.  So I managed to make pretty much the same program but with less than half the number of lines! πŸŽ‰πŸŽ‰πŸŽ‰

Below is the resulting outcome from the new code:


I hope you enjoyed my new post and hopefully it has helped write shorter codes that is just as useful!  If you have any suggestions for future posts, or you know better ways to improve my codes, let me know!  

P.S.  
Did you know that computers have an in-built snipping tool?  It has been a life saver when writing this post so now I don't have to go through processing the images as I used to for the previous posts πŸ˜…  For my friend who let me know that this tool exists, you know who you are, THANK YOU πŸ’–  For my readers, I hope you find the new image formats easy to look at, and let me know if you have any issues πŸ‘Œ

Tuesday, January 12, 2021

Storytime: What is programming?

 Hello World!


If you've been wondering why I haven't posted anything for December... well it's because I've been busy with student life πŸ“šπŸ“šπŸ“š  I did however update my past posts to include PDF versions of the codes, so check them out!  


Programming can be a pretty alien concept.  You see it in some movies, and it looks cool, but I don't think many people know what codes actually are.  I didn't know anything about codes, or Python, or anything of the sort before I actually started learning it myself.  That's why I decided to write a chill story about coding.


Humans have a lot of diversity in thoughts, beliefs, and language.  The language doesn't have to be too rigid for humans to understand each other as long as they can get the "gist" of it.  In fact, a language can emerge, change, or even disappear to be replaced with a new language.  Humans have ideals and ideas to reach their goals.  Humans get bored easily so they hate repetitive and tedious tasks.  Humans make mistakes as well.  That's why computers are very useful.  

Computers exist to complete repetitive tasks and not complain about it.  Computers are also super SUPER fast in doing so.  But computers don't learn or adapt so they need to be told in a specific way to understand what their task is.  Humans envision the goal and computers execute the tasks to get there.  There still needs to be some mode of communication between the human and the computer, so that's when programming languages, like Python, come in.


Programming languages allow the computer to understand what the human wants done.  They have in-built phrases that act as commands or specifiers to grant the human their desires.  In Python, commands would be something like "print" while specifiers would be....  "while" or "True"

For example, if the human wants the computer to write out "Hello World":

  1. Human opens Python 
  2. Human types in print("Hello World")
  3. Human presses Enter
  4. Python "talks" to the computer
  5. The computer responds by printing out "Hello World"

Simple, right?  We learn the Python Language so that we can better communicate to the computer.

Now the hard part honestly is.... making mistakes.  As stated before, computers need the codes to be written in a specific way to understand them.  Humans are not perfect beings so we make mistakes or say things "incorrectly."  Sometimes it can feel like Python is being mean when they respond with an error after typing in hundreds of lines of code, but that's just Python saying "I'm sorry, I don't understand.  Can you please rephrase that?"  Remember, computers can't learn but humans can, so we just have to learn to work with Python and computers. πŸ˜…

I hope this post helps in understanding programming better.  I still have lots to learn myself, so if any of you can come up with good analogies to describe coding, feel free to comment!!  🐍🐍🐍


Thursday, November 19, 2020

Project 2: Guess the Number

 Project 2:  Guess the Number


This time, I present a code in which you can play Guess the Number!  In the game, the code will "think" of a number from 1 to 10, and the player has to type in what number that would be.  This code helps you learn a few new fun features😁


Key learning outcomes:

  • Loops
    • while
    • break/continue
  • Conditions
    • if/elif/else
  • Error proofing
    • try/except

Loops
Loops are used for repeating the same task over and over.  In Guess the Number, it's used to prompt the player to guess what number the code has chosen.
  • while
    • Acts as the starting point for the loop
    • Allows the loop to continue or break depending on the conditions set out in the code
  • break
    • "Breaks" the code when prompted
  • continue
    • "Continues" the code when prompted
    • Returns back to starting point

Conditions
Conditions are used to split a scenario into different courses of action depending on the input.  In Guess the Number, it's used to print out a specific text depending on how close the player is to the number chosen by the code.
  • if
    • The "most important" condition
  • elif
    • Short for "or else if"
    • The "second-most important" condition
  • else
    • The "least-important" condition

To showcase how loops and conditions would be used, here is the prototype of Guess the Number.  (Alternatively for the PDF version, click here)


Here, while True means "while y is an input and is then converted into an integer". 
break means break the loop.
continue means continue the loop.

if means "if the player enters the right number", print "Congratulations!"
elif means "or else if" the player inputs a number in a given range, then they would receive a message saying "Error, out of range.", "Close!", "Too big", or "Too small". else means if the player enters any other kind of input, they would receive a message saying "Error, please check input."

IN THEORY, this code should have worked to play the game... but humans don't always understand that computers don't recognise numerical numbers and word-based numbers.  See what happened when I tried playing the game using the code above:  (Alternatively for the PDF version, click here)

The message in red means that Python couldn't convert seven into an integer.  When humans look at "seven", it still means 7, but Python sees "seven" as five letters "s-e-v-e-n".  In order for Python to convert the player's input as 7, the player has to type in "7".  This error means that the code didn't work and the player has to type in the code all over again to play again.  But this can be annoying and the player might not want to play anymore.  Is there a way to prevent this?

In fact, the "try-except" can do just that!

Error proofing
  • try
    • "try" to execute the code as planned
  • except
    • If the code didn't work as planned, do this instead

Here is the new error proofed code: 
(Alternatively for the PDF version, click here)

By including try and except in Guess the Number, even if the player types in "seven" instead of "7", the code will still be able to run and the player will realize that they have to type in the number as a number and continue on playing.  Here's what happened when I tried playing the game with the improved code:
(Alternatively for the PDF version, click here)

See?  When I typed in seven this time, the code can just ask me to check what I typed in, and I can continue on playing!


I hope you liked my post!  Have a go at making your own version of Guess the Number and practice your loops, conditions, and error proofing 😎



P.S.
In case you were wondering...
  • import random
    • "imports" a library that allows the program to choose a "random" number
  • x=random.randint(1,10)
    • Restricts the "random" number to choose from to between 1 and 10
    • x is the random number chosen
  • #
    • Is NOT actually part of the functional code
    • Acts as a sticky note for the programmer so that they can keep track of what that specific piece of the code means as they are planning/writing the whole code
P.P.S
If you don't know or remember what "def" or "print" means, check out my previous post under Project 1.
















Friday, October 16, 2020

Project 1: Hello World!

Project 1:  Hello World!

For my first project, I present a mad lib generator!  Writing codes for the first time can seem daunting, but practice makes perfect.  Building a mad lib generator is probably one of the simplest, if not the simplest, assignment to take on when writing code, so definitely give it a try!

Merits on making mad lib generators:
  • Allows you to learn the most basic functions by heart
  • There are many mad libs online to choose from if you find it difficult to make a story of your own
  • It's very fun to play mad libs with friends!

Below is my mad lib code:
(Alternatively, if you would prefer to view the PDF version, click here)


A review of the functions used:

  • def
    • Short for "define"
    • Used to "define" (or name) a new function that the programmer creates
    • Allows the function to be used again and again
      • It is useful for playing the same mad lib again with different people or different word choices
  • print()
    • "Prints" out whatever is contained within the bracket
    • When writing a string in the bracket, make sure to use quotation marks (") or (') or else you may get an error
      • String is a sequence of characters
  • input()
    • Prompts the user to "input" a string 
    • e.g. f=input("Enter name: ")
      • The user is prompted to enter a name
      • f now means the name entered
      • This becomes relevant in print(f) in which the name is printed out in the final line
Other points to keep in mind:
  • +
    • Used for adding strings together (also called "concatenation")
    • For example
      • print("bull")
      • bull
      • print("dog")
      • dog
      • print("bull" + "dog")
      • bulldog
  • ,
    • Used for making a space in between strings
    • For example
      • print("bull")
      • bull
      • print("dog")
      • dog
      • print("bull" , "dog")
      • bull dog

Here is the mad lib generated with my word choices.  Once you've had a good laugh, try and write codes to make your own mad libs and laugh some more!
(Alternatively for the PDF version, click here)









A New Frontier: Building bots without code!!!

 Dear Readers,  Welcome back to this month's Chronicles of a Neurodivergent Programmer.  Last month, I took a break from writing about t...