In this tutorial, we’ll learn how to create a number guess game in Python. We’ll be implementing the program step by step with proper explanation in order for better understanding.
The complete source code for this guessing game will be provided in the end.
Outline
- Introduction: Number Guess Game In Python
- Creating Number Guess Game In Python (Step By Step)
- Number Guess Game In Python Complete Source Code
- Conclusion
Introduction: Number Guess Game In Python
We’ll be creating a program in which we’ll first show the range or numbers, the user has to guess from. Then we’ll give 3 chances to the user in order to choose the right number. If the right answer is not chosen even after three attempts, then a failed message will be displayed. Or else a congratulations message will be shown.
Let’s now practically understand its implementation.
Creating Number Guess Game In Python (Step By Step)
Follow below steps in order to properly understand how a number guessing game in Python is created programmatically.
Step 1: Display The Range Of Numbers
print('Choose the right number from 0 to 9')
This will show a valid range or numbers that the user should choose from.
Step 2: Specify The Right Number
theRightNumber=7
We’ve stored the number 7 in a variable. You can use it directly if you want. Later in the code, we’ll use this variable in a condition to check if the picked number matches with that number or not.
Step 3: Specify Number Of Attempts
We’re using Python while loop to iterate the number of attempts. Click here to learn Python while loop in detail. We’ve specified the number and limit of attempts using two variables. See below:
numberOfAttempts= 0 limitOfAttempts= 3
Each time the body of while loop is executed, the number of attempts will be incremented by 1.
Step 4: While Loop
while(numberOfAttempts < limitOfAttempts):
This while loop will keep on iterating while the number of attempts are less than the limit of attempts.
Step 5: Get Value From User
pickedNumber= int( input('Enter the number:') )
This comes inside the body of while loop. It’ll take an input from the user. By default, it’s in string format so we’ll convert it to an integer using the int() function. Finally, we’ll store it in a variable.
Step 6: Increment The Number Of Attempts
numberOfAttempts= numberOfAttempts + 1
This will add 1 to existing value of number of attempts.
Step 7: Entered Number Validation
if pickedNumber==theRightNumber: print(f'Congratulations, you\'ve picked the right number which is {pickedNumber}.') break
This Python if statement will check if the entered number matches will the right number, then it’ll show the congratulations message. Also, it’ll break the loop so it won’t be executed again as the right answer has been picked.
Step 8: All Attempts Failed
else: print('Sorry, all the attempts were wrong.')
Python while loop has an else statement as well. It’ll get executed when no valid attempt has been made.
Step 9: Test The Code
Let’s now test our program and see if we get the desired output or not.
Attempt 1
Choose the right number from 0 to 9 Enter the number: 4 Enter the number: 2 Enter the number: 8 Sorry, all the attempts were wrong.
Attempt 2
Choose the right number from 0 to 9 Enter the number: 3 Enter the number: 7 Congratulations, you've picked the right number which is 7.
So this is how we can make a number guess game in Python. Hope you like this post and have learnt a lot from it. Feel free to share it with your squad of Python developers.
Don’t hesitate to ask if you still have any questions related to this number guess game in Python. We’ll be super glad to answer all your questions.
The complete source code of this number guess game in Python is given in the below section.
Number Guess Game In Python Complete Source Code
print('Choose the right number from 0 to 9') theRightNumber=7 numberOfAttempts=0 limitOfAttempts=3 while(numberOfAttempts<limitOfAttempts): pickedNumber=int(input('Enter the number:')) numberOfAttempts=numberOfAttempts+1 if pickedNumber==theRightNumber: print(f'Congratulations, you\'ve picked the right number and it\'s {pickedNumber}.') break else: # this else block comes with the while loop print('Sorry, all the attempts were wrong.')
Conclusion
To conclude this article, hope you now have an in-depth practical understanding of how to create number guess game in Python. We strongly encourage you to give your feedback on this post.
Do visit our other posts on Python programming. Thank you for reading this article.