Python Practice for Beginners: Build a Number Guessing Game | Navid Ansari | Skillshare

Playback Speed


1.0x


  • 0.5x
  • 0.75x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 1.75x
  • 2x

Python Practice for Beginners: Build a Number Guessing Game

teacher avatar Navid Ansari

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      01 Guessing game introduction

      0:36

    • 2.

      02 Create the secret number

      2:51

    • 3.

      03 Dont let user to put non integer value in input

      3:28

    • 4.

      04 Dont let user to get out of limit of secret number

      2:36

    • 5.

      05 Game logic

      3:03

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

1

Student

--

Projects

About This Class

Welcome to Python Practice for Beginners! In this hands-on class, you'll learn fundamental Python programming concepts by building a fun and interactive Number Guessing Game.

This course is perfect for absolute beginners or those looking to strengthen their Python basics. You’ll learn:
 Working with variables and user input
 Using loops and conditional statements (while, if/else)
 Generating random numbers
 Basic error handling and input validation

By the end, you’ll have a fully functional game that challenges players to guess a secret number while providing hints. No prior experience needed—just bring your curiosity!

Meet Your Teacher

Teacher Profile Image

Navid Ansari

Teacher

Hello, I'm Navid.

See full profile

Level: Beginner

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. 01 Guessing game introduction: The dosers we want to create a guessing game, number guessing game. So we create a secret number, and users should guess what number we are thinking of, right? So if they put a number that is less than that secret number, we tell them that it's less than secret number. And if the number is bigger than the secret number, we tell them that it is bigger than number. And the goal is to find that secret number with less attempt possible. Let's see how we can do. 2. 02 Create the secret number: So now for creating or guessing game. For doing that, I want to go to desktop in here, create a new folder, and I want to call it guessing game, right? And with that, I will go to VS code, go in here and open up that folder. So in here, it is in desktop. It's called Guessing Game head Select Folder, and it will open it for us. And in here, let's just create a file. I want to call it again, guessing. Game dot PY with that. Let's just zoom a little bit so you can see it better. That's it. Now with that down, first of all, we want to tell users what this application is doing. So I will use a print for that, and in there, use a double quotation. I will say, welcome to the number guessing game, right? Now with that, I want to use another print, and I want to tell the users what this game is about. For example, um thinking of a number 1-100, for example. So with that done, they know we are thinking of a number, and they should guess after that. But for no, this is good. Now, we need to generate a random number 1-100. For doing that, there is a library in Python that is called random. And I can say import random. And with that, I can say random dot, rand, It, right? An integer 1-100. And after that open and close parenssF the minimum and maximum, we can put one and 100. That's it. And this rand int will give us an integer in return, and we can save it inside a variable. This variable, I would call it secret number, right? It's equal to this random integer. Just with that, I want to use a print to show this secret number first. Secret number. Of course, we don't show it at the end game. Now with that done, I want to go here, go to terminal and create a new terminal. And in here, I want to say PY at tab guessing game head Inter, and no, it says, welcome to the number guessing game. I'm thinking of a number 1-100 and right now, the secret number is 32. If you run it again, it will give us another number 53. Again, if you run it, it says 71. So each time that we run our application, it will give us a secret number. We don't want to show it like this. No, we want to tell the user to take a guess. Let's see if we can do that in next. 3. 03 Dont let user to put non integer value in input: No we have a secret random number. No, the user should take a guess, and for doing that, we should count the amount of guess that he or she have done, right? So for doing that, I will create a variable. I will call it attempts, it's equal to zero. And no, I want to create a wilt loop to take the guesses of the user. So we say Wil true so this wire loop always happen, right? Now, in here from input, we want to get that guess, right? So in here, I will create input, and in here, I will say, take a guess, right after that colon and a space. So it looks more better, right? And this will give us a text or a string, right? So we can save it somewhere. I will call it guess, and guess is equal whatever that user write in the input, right? With that first thing that I want to check, I want to check if they put a integer value, a number, right? So for doing that, we can say, I guess, Dot is digit, right? With this function, what it will give us if we hover over it, it will give us a boolean. Return true if the string is a digit string false otherwise, right? So if the is digit was true, in that case, it means the user write a number, right? But I don't want to do it like this. I want to check if it is false. So if the user write a non digit text input, we check if guess that is digit will be false. So in here, if is digit is equal to false, what do you want to do? We want to say first a print and say, please enter a valid number 1-100, right? And after that, we say continue. So when this continue happen, it will go to the beginning of this loop and it will tell the user to take a guess again. Now, let's just test that I here. Let's run. You can see it says, I'm thinking of a number 1-100. We should take a guess now if we put 50 you can see, again, it will tell us to take a guess, but if you put N in there because it is not a number, if you had interno, it says, Please enter a valid number. It will give us an error. We can just put an error in here like this, so it will be more obvious. So in here, Control C to get out of the application. After that, let's just run it again. For example, 50, it's okay. It won't give us any error. But if you put N in there because it's not a digit, it's not an integer, if you had inter, it says error, please enter a valid number 1-100. So now with this line of code, we are just getting some integer value from input. 4. 04 Dont let user to get out of limit of secret number: Know what I want to do because after this I guess is digit is equal to false, we are sure that the guess is a number. We need to convert it to a, for example, integer, right? So we say int, open and close parenths and put the guess there. So this will convert the number string that user write in input to a integer. And we can save it inside itself. We say guess is equal to in guess, right? So with this right now, this guess is a string, but in here, we are changing it to an integer. Now we are working with the integer. And with that, I want to check something. Guess is less than one, or guess is more than 100. It means the user put a value that is not 1-100. We tell it to please put a number 1-100, right? In that case, we want to tell them with a print your guess should be 1-100. After that, we want to say continue, go back to the beginning of this y loop and take another guess. Now, with that, let's just test that out. If we run a code in here, it will tell us to take a guess. If we put N in there, it will give us error. And no, if we put ten, it won't give us any error, but if we put 110, it will tell us your guess should be 1-100. And again, in here, I want to put an error in here, so it will be obvious that they did make a mistake, right? Again, with that in here, Control C to exit out of the application, and again, run it. Now, if you put 110, it says, error, your guess should be 1-100. Know with that done if we reach this line over here and it didn't return to take a guess at the beginning of or I loop, it means guess is an integer value that is 1-100. And now we can check if the number is close to secret number or not, if it is bigger or smaller than that. Let's see what we can do that. Next. 5. 05 Game logic: No, we want to give the user some hints if they are close to the secret number, right? For doing that, first thing that I want to do, I want to increase these attempts, right? So I will say attempts is equal to attempts plus one, right? That's it. We increase the attempts. No, we want to check I guess. Is equal to the secret number. We can tell the user, congratulations. So in here, we can print with a FS string, right? Congratulation. You guess the number correctly in open and close curly brackets, attempts. You want to show how many attempts they have done. Attempts. That's it. Now, after this, we want to break out of the loop and finish the execution, right? But if the guess is not equal to the secret number, we want to check it with Al if, right? I guess was less than the secret number. In that case, again, I want to use another print, and I want to tell the user too low. Try higher value, right? And if the guess is not secret number and the guess is not less than secret number, so the guess is bigger than secret number. So in here, we can just use se state and a colon. And in here, we can just say print, and in here, we say two high, try, lower number, right? Just like that. Let's just use Control C on terminal, so exit out of the application. And after that, make sure you save and let's just run. And in here, it's saying, I'm thinking of a number 1-100. I will say 50. It says try lower number. So I will say 20. No, it says, try lower number again. I will say ten. Again, it says, try lower number. Let's just say five, again, four, three, and no. The secret number was three, and it says, congratulations. You guess the number correctly in six attempts. Let's just run it one more time. I'm thinking of a number 1-100. Take a guess, again, 50, try higher, 60, again, higher. Let's just say 80, 70, 75, 77, 76. And no, the secret number was 76, and congratulations. You guessed the number correctly in seven attempts. That's it. Now game is ready.