Random Number Guessing Game - Python (Beginners)
Key Takeaways
This video tutorial demonstrates how to create a random number guessing game in Python, covering topics such as random number generation, input validation, and while loops using tools like random.randint(), int(), and isdigit().
Full Transcript
hey guys and welcome back to another YouTube video so in today's video I'm going to be showing you how to make a random number guessing game in Python this is more of a beginner tutorial and this is for some of the viewers on my channel that maybe aren't us advanced with Python and don't know about classes and functions but this will allow you to learn a lot especially if you're a beginner and we're gonna go through the qual loops generating a random number getting input from the user checking that input and then obviously checking if that guess is correct versus the numbers so let's go ahead and get started so the first thing I want to do here is I'm just gonna import a module and this is random I'm gonna show you quickly how we can create a random number in Python that's why some of you might be here so to create a random number it's quite easy you just create a variable so our I would say is go to Rand int and then in here I just put two bounds and a bound is just like a start and end so if I put something like 1 4 and then I print R to the screen I will get a number between 1 and 4 and that includes 1 and 4 as well so it'll randomly pick one so we can see here I get 2 if I run it again I get 4 keep running it you can see that we continually get random numbers so that's kind of the basis of what we're gonna need obviously to make a random number so the first thing I want to do when I'm coding this game is I'm just gonna set up a basic while loop and I want to ask the user to tell me how difficult I want the game to be and the way we're gonna do that is we're gonna ask them on what number they want to be able to guess up to so starting at 1 and then what number to guess up to so obviously the larger number they pick the heart of the game is gonna be so I'm going to make a while loop here so while flag and flag is set up to true and then say num is equal to input and type a number for an upper bound okay I know that sounds complicated but just means like what we guess up to so this is gonna grab our input from the user store it in the variable num so now what I want to do is I want to check if this variable num is a integer or not because if they don't type in a number well I don't want to allow them to continue at the game because we don't know what number we should go up to so the way we do this is actually really easy and so we can do an if statement I say if num oops one my typing bum they're numb nut is digit like this and what this is gonna check is if this string because whenever we grab input from the user comes in a string is equal to a number then we're gonna move on and we're gonna say okay let's play the game here's what we're guessing up to so on now if they don't say that I'm going to set up an else here and we'll go into that in a second so if this is a digit they've typed in some correct input we're gonna say print and I don't know how I did that I'll say let's play like that okay so let's play let's an exclamation point here as well and now what I'm gonna do is I'm gonna convert this into an integer and the way that we can do that just say Namie equals int num and then I'm going to say flag is equal to false so that we can get out of this while loop and then down here so where my mouse is kind of hovering is where we're gonna actually play the real game up here is just where we're getting the input for what to guess up to so let's say they don't type in a valid number they type in like hello for the upper bound well we don't know what to do with that so we're simply gonna tell them invalid input like that and I know I spelled invalid wrong just ignore it for now try again like that okay so let's fix this invalid input try again and then this is gonna prompt them to continually type in numbers until they eventually give us a number that is a digit or a string whatever it that is a digit and it will print let's play and we'll move forward so let's just test this out before you look too far so we say type a number I'm gonna say hi it's gonna say invalid input try again let's do let's try this negative seven invalid input try again how about seventy six there we go let's play and that works now the reason negatives don't work because technically a dash is not a digit and we only want numbers that are above one anyway so this actually works well for us okay so now we're going to start our main game so we need to generate a number that the user is gonna try to guess within their bound so to do this I'm gonna type secret let's give it a variable name for the number because it is gonna be a secret to the user I type random dot R and int 1 and then that bound so that bound is num and that is what they typed in and we've already made sure that this is indeed a number and it's not a string so this will work well in our random grant int okay so now I'm gonna set up two variables I'm gonna say guess is equal to none and I'm gonna say count is equal to 1 I'll go over what these do in just a minute so pretty much our guess is just set to none by default because we haven't yet made a guess and that our count is equal to 1 because we want to tell the user how many times it took them to get the correct number so I'm gonna set up another while loop now and the condition I'm going to use for this is a little different than some of you might typically use I'm gonna say well guess does not equal secret like that and that's gonna say we're gonna continually keep guessing until our guests ends up being secret or like the number that we're trying to guess which is secret like this and in that case we will stop and we will do whatever comes after ok so now I'm going to say in here I want to ask the user to guess so to do this we're gonna say guest is equal to input and then same thing here please type a number and I'm just gonna say good between 1 and and now I'm gonna put in the number that they typed in so num between 1 and num and we'll do another comma like this and I'm just gonna put a colon actually I don't think I could put calm as an input sir we're just gonna have to ignore that for one second because it's gonna crash you just have to do like this ok so what I've done here because in the print statement we're allowed to use commas in the input statement we're not so we have to make sure everything is a string so we're gonna say please type a number between 1 and and then we're gonna convert our number into a string and then we're gonna add a colon here so that they can kind of type after it and these spaces are important otherwise you're gonna see they'll be mushed together if you don't have the space after the quotation marks ok so now that we've done that we want to do the same thing that we did up here and make sure that this guess is indeed a digit so let's say if num it's I guess not not guess done is digit like this then we will convert our guests into an integer so guess equals int guess like that now the reason that we need to do this and check if it's a digit and I'll show I'll show you when I open up the editor here okay so let's just get through this like 54 oh okay it's just gonna keep going anyways if I open up ideally I can show you I spoke later just give me a second okay so let's say if I say X is equal to hello like that and then I put something like this and I say int X well it says in built in a roll for int with base 10 so for example if we didn't check if someone our if what they typed in was an integer and we tried to convert it into an int it would crash our program and obviously we don't want our program to crash we want to just keep asking until eventually they type in a correct number so that's why we need to do that and now I'm gonna say if guess equals equals secret like that then we can tell the user well you guessed the correct number so we'll say you've got it and I don't know I can't type today okay you got it like that otherwise so if the user did not guess the number correctly we're gonna say please try again like that now one other thing we need to do here and some of you may have already thought of it is we have this variable count so if the user gets it wrong we won't add one to our count like this indicating that they're getting another try to guess the number okay so I know I went kind of fast I'm just going to type one last line then we're gonna test the program and go through it one more time because anyone's confused okay so I'm just gonna print down here because once the guess is not equal to so once the guess is equal to secret we're not gonna be going through this wall loop anymore because this condition is not true because the two things will indeed be the same value and we're gonna print what comes so what comes out for this while loop is the print statement and we're just gonna say it took you this many tries it took you in here we can use commas count and that will say guesses like that with an exclamation point so let's go ahead and run the program and it's gonna say type a number for an up bound I don't want to be here for too long so I'm going to type six once up say let's play please type a number between one and six type four please try again okay I look three okay that didn't work to one oh is it really gonna be the last one six you got it it took you six guesses and there we go that is indeed our game and you can play this as many times as you you want and if you wanted to continually keep playing this and have it to keep asking you and keep asking you to type in a number and then - sorry type in a number for an upper bound and then type in another number and try to guess it you can put another while statement so I'll put something like this well true if you take all this and indent it now we'll do this quickly so if I type it upper bound three and I type one you got it it took you one guesses or I guess I should probably say guess not guesses anyways type a number for an upper bound so it's asking me again so I'll type 3 type number 21 and 3 type 2 you got it it took you one guess and you can continually and continually keep playing and this will never stop so if you keep if you can play as long as you want and if you want to exit this you can just click the red X or you can click ctrl C on your keyboard and that will break the while loop because this is indeed and infinitely anyways that's been it for this video I hope you guys enjoyed and you now know how to make a random number just guessing game if you did please make sure you leave a like and subscribe and I'll see you again in the next video [Music]
Original Description
In this video I will be explaining how to create a random number guessing game in python. This is a beginner tutorial and will walk through step by step how everything is coded. In this video you will learn how to create a random number in python. How to to use while loops. How to check if values are equivalent and how to see if a string is a representation of a digit.
Want To Support This Channel?
Bitcoin: 1PbkAYLFaJBgjbKn2ptGyBz65xWN8hJgBU
Ethereum: 0xdd42dbbdba60f7163fc7a840e189474b6e8bfcad
Ripple: rD4arM9CVjQWqi8f1kxdpCgkCgEkqBgtud
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech
- Tech With Tim
- Programming
- Coding
- Pygame
- Python Tutorials
- Random numbers python
- Random number guessing game python
- Beginner coding
- Beginner coding tutorial python
- How to make games in python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Prompt Craft
View skill →
🎓
Tutor Explanation
DeepCamp AI