DEV Community

Naivedh
Naivedh

Posted on • Edited on

Guess Game in Python

Hey Guys, So are you ready for your first python project? Then let’s get on with it, I would recommend you to code along. This game will help you clear out your basics such as Input-Output, Random module, Loops, if… else.

First import random module.

Alt Text

Next we will use randint function from Random module for generating a random number between 1 to 100.

Alt Text

Now, Let’s take input from user. Input is in the form of string so we will convert it to int using int().

Alt Text

Let’s use while loop to compare the value of user and computer again and again until they are equal. Do not forget to add colon(:) at the end. You can also use for loop and give limited number of chances to User.

Alt Text

Now, we will use if … elif programming constructs to match the numbers. We will use 4 statement to check the number is high, a little high, low or a little low. Take a difference of 10(i.e. if difference is greater then 10 then print go high or go low else print go little high or go little low ) to make game a little easy. Don’t forget to take input again if value doesn’t match.

Alt Text

Great work! You have created your first project.You can get the entire code here. You can even add more to it such as add a for loop instead of while loop for 10 inputs. Give a Scorecard at the end of game. If User Guesses the Number in first attempt then he gets 100 points, if in 3rd attempt he/she gets 70 points, if in 6th he/she gets 40 points and so on. . .

You can create the same project in different languages too. Do try it out if you are interested in other languages.

Note: Do not forget colon (:) after for, while or if else statement. Take care of indentation too.

Top comments (0)