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.
















LLM Part 2: Encoding

Welcome to Part 2 of building your own large language model! Part 1 was about breaking down your input text into smaller subwords. (tokeniza...