...

Number Guessing Game

As part of this project you will learn:

What is computer programming (coding)?

Computer Programming or Coding is the process of writing instructions for a computer to follow. It's like a recipe, but for a computer. The instructions tell the computer what to do and in what order to do it.

Those sets of instructions that the computer follows to do something are written in a Programming Language.

Next
Python Programming Language

We will be learning a text-based programming language called Python.

Why Python?

  • It's perfect for beginners.
  • It's easy to setup.
  • It's powerful
  • It can be used for graphics, animations, web applications, games, etc.
  • What is Trinket?

    During our Coding Classes, we will be using Trinket. Trinket is an online version of code editor where students can write and run their code online. Trinket is available online at trinket.io.

    Students may create a free account to save all the projects-> click here, and note that parental permission is required to create accounts for children under 13.

    IMPORTANT!: It's important to understand that Trinket creates public web pages and projects that anyone with a link can view. Young people may need a reminder about the importance of not sharing personal information online.

    Python Syntax

    Python has a simple syntax similar to the English language. Understanding the basic symbols in programming is important. We will frequently come across these symbols in our coding classes.


    Prev Next
    Please ensure all required fields are filled in
    Step 1: Import the Random module
  • Open the blank Python template trinket: Open New.
  • In order to generate a random number for the user to guess, we need to use the random module. To import this module, add the following line to the top of your file:
  • Always place import statements at the top of your code.

    Prev Next
    Please ensure all required fields are filled in
    Step 2: Generate a random number

    To generate a random number between 1 and 100, we will use the randint() function from the random module. Add the following line to your code to generate a random number:

    This will assign a random integer between 1 and 100 to the variable secret_number.

    Prev Next
    Please ensure all required fields are filled in
    Step 3: Ask to guess the number

    To ask the user to guess the secret number, we will use the print() and input() functions. In Python, the input() function is used to get input from the user, and the print() function is used to display output to the user.

    The int() function is used to convert a string to an integer. In this project, we use the int() function to convert the user's input (which is initially a string) to an integer so that we can compare it with the secret number.

    Add the following lines to your code:

    Run your Code. This will display a message to the user asking them to guess the secret number, and will then get their guess as input from the keyboard.

    Prev Next
    Please ensure all required fields are filled in
    Step 4: Use a while loop and if..else to keep asking to guess the number
  • A loop is a programming structure that allows us to repeat a block of code until a certain condition is met. In this lesson, we used a while loop to keep asking the user to guess the secret number until they got it right.
  • Conditional statements allow us to execute different blocks of code depending on whether a certain condition is true or false. In this lesson, we used an if...else statement to print "Too low!" or "Too high!" depending on whether the user's guess was too low or too high.
  • We will be using != and > operators to compare the user's guess with the secret number. The != operator is used to check whether two values are not equal to each other. In the while loop of our number guessing game, we used the != operator to keep looping until the user's guess was equal to the secret number. The condition guess != secret_number was true as long as the user's guess was not equal to the secret number.

  • The > operator is used to check whether one value is greater than another value. In our number guessing game, we used the > operator to determine whether the user's guess was too high. If the user's guess was greater than the secret number, we printed "Too high!" to the screen.
  • Add the following lines to your code:

    Run your Code. This will keep asking the user to guess the secret number until their guess is correct. If their guess is too low, it will print "Too low!" on the screen. If their guess is too high, it will print "Too high!" on the screen. The guess variable will be updated with their new guess each time through the loop.

    Prev Next
    Please ensure all required fields are filled in
    Step 5: Congratulate the user when they guess the number correctly

    Add a print statement to congratulate the user when they guess the number correctly. You can do this by adding the following line of code on the same level with a while loop:

    Prev Next
    Please ensure all required fields are filled in
    Step 6: Save your work

  • If you don't have a Trinket account, click the down arrow and then click Link. This will give you a link that you can save and come back to later. Or Click Email to send this project to your email address.
  • You'll need to do this every time you make changes, as the link will change!
  • If you have a Trinket account, you can click Remix to save your own copy of the trinket.
  • Prev Next
    Please ensure all required fields are filled in
    Step 7: Complete and Share

    Submit your final project



    Copy your Trinket Url

  • Click Share->Link
  • Copy the link to share your code
  • Paste the link into the field
  • Prev