Guess the Number (Computer)

 Guess the number (computer)

Difficulty: Easy

What is this Project

        This is a beginner project for people starting with python. The user will pick a number between 1-100 and the computer will try to guess it by picking a random number between 1-100. The computer gets three tries and either wins or loses. Great for people starting python.

What's in this Project

        In this project, you will be working with a couple of things that you may need to know. If you need to learn any of them, click on it and it will take you to a reliable resource that's quick and easy. First up, we'll be using Python's random module. This is a module made for python that basically allows the developer to make a random choice between a list, set of numbers, etc. We will be using random's randint() method in this project which picks out a random number from a given two numbers (including those two numbers). This task also includes while loops, conditionals, and getting user input. You can learn all of these in-depth after this project or before. To learn anything though, you need an understanding of the course materials.

Files

  • main.py

Step 1: Importing

        This step is fairly easy and all you need to do is import the random module. If you checked out my post on the random module (click the link in the "What's in this Project" section), you know that you won't need to install anything as random come pre-installed with python. You can either import just the randint method or import the whole random module. Do this step yourself and see the solution below ONCE YOU ARE DONE.



Step 2: Asking the User to Pick a Number

        In this step, you will be using Python's built-in input() function to ask the user to pick a number for the computer to guess. The number will be between 1 and 100 (including those numbers). You will need to check if the number is in between those two numbers or the computer will never be able to guess. Here is a hint, you will need a while loop to check the number entered by the user. Check the solution located below once you have completed this step.



Step 3: Create the While Loop

        Step 3 is creating the while loop. Try and do this yourself before you go onto the solution which can be found at the end of this document. The while loop will be based on a variable called guesses. The variable guesses is defined outside the while loop and starts off with a value of 0. The while loop itself is next and it will run only until guesses is equal to 3. Go and do this now.



Step 4: Pick a Number as the Computers Guess and Check it Against the User's Number

        This step finally uses the randint() method in the random module. If you don't know what it is, click the following link to go to my post where you will learn about random briefly and three useful methods, one of which is randint(): Python's random Module. We'll be using randint() to generate a random number between 1 and 100 to use as the computer's guess. This will be saved in a variable called computer_guess. Check computer_guess against the user input and if it is correct, print something and break the while loop using the break command. If the guess isn't correct, add 1 to the guesses variable so the while loop will still run.



Step 5: Check if the Computer Guessed 3 Times

        This is the end of the project. You wouldn't want to end the computer game if it fails by just ending the program. So your challenge is to add a goodbye message when the computer is out of tries. The solution is below.

Full Code

        If you want to compare your code with my code, you can take a look at it through this link: GitHub Gists Full Code. You can make edits to your code if you want. It's your project! Change things, and more complex parts like difficulties or changing the computer's amount of tries from user input. Practicing python through interactive projects can get you to many places. That's where it got me. Check out my other posts and leave a comment for any feedback and changes I should make.

Comments