Some random integer value is selected by the computer system and the user has to guess that four-digit integer number. We will use Python language.

Any four digits random integer number will be selected by the system and the user has to guess that random integer number.

In this tutorial, we will guide you through each step of making this interactive number guess program.

Algorithm:

1. Compiler select a random inter four digits number between the range 1000 to 10000 which stores it in a variable.

2. User has to enter a four-digit number.

3. compiler compares each and every digit of entered number with the guessed number.

4. IF a single digit is correct from guessed number then the user gets the output like "Not quite the number. But you did get 1 digit(s) correct!

Also, these numbers in your input were correct (digit)".

5. Else If two digits are correct from guessed number then the user gets the output like "But you did get 2 digit(s) correct!

Also, these numbers in your input were correct (digits)".

6. If the user guessed the correct number then the compiler shows the output like "You will be best on this game!

It took you (no_of_tries) tries to be a winner of this game".

7. Else if the user didn't guess the correct number then the user will get the output from the compiler like "None of the numbers in your input match".

To generate a random number we will use a python module random. First, we have to import this module.

`import random`

After importing a random module we will generate a number range between 1000 to 10000 and store it in a variable num.

`num = random.randrange(1000, 10000)`

We take the input from the user and store it to a variable n.

`n = int(input("Guess the 4 digit number:"))`

After taking input from the user we create a ctr variable to store the number of tries the user takes to guess the number. we assign 0 value to this variable.

` ctr = 0`

We add one conditional statement.

if (n == num): print("Great! You guessed the number in just 1 try! You're best in this game")

It checks the equality of the guess. If the result is true then it will get terminated and If not true then it will go to the else part.

now, in the else part we are defining the while loop.

else:

while (n != num): ctr += 1 count = 0 n = str(n)

num = str(num)

correct = []

In the first statement of the while loop, we are defining the control expression.

In the next line, we are incrementing the value of ctr by 1.

After that, we are performing here the explicit type conversion of an integer value to a string in order to the east an extraction of digits and then we perform explicit type conversion of string to integer

correct list stores the digits which are correct.

Below the while loop, we are defining one for loop.

for i in range(0, 4): if (n[i] == num[i]):

count += 1 correct.append(n[i]) else: continue

In this for loop, we are checking the quality of digits. If the number of digits is guessed correctly then increments the count. And correct digits are stored in the correct list.

And if all digits are not correct then continue the process.

We are defining another condition statement.

if (count < 4) and (count != 0): print("Not quite the number. But you did get ", count, " digit(s) correct!") print("Also these numbers in your input were correct.") for k in correct: print(k, end=' ') print('\n') print('\n') n = int(input("Enter your next choice of numbers: ")) elif (count == 0): print("None of the numbers in your input match.") n = int(input("Enter your next choice of numbers: "))

This block of code gives the output:

If you didn't guess any digit correctly then it shows the output like :

None of the numbers in your input match.

Enter your next choice of numbers:

And If you guess suppose 2 digits correctly then it shows the output like:

Not quite the number. But you did get 2 digit(s) correct!

Also these numbers in your input were correct.

2 4

At last, we are defining one conditional statement, it will get executed when you guess the correct number.

if n == num: print("You will be best on this game!") print("It took you", ctr, "tries to be a winner of this game.")

Now let's see the whole code

import random num = random.randrange(1000, 10000) n = int(input("Guess the 4 digit number:")) if (n == num): print("Great! You guessed the number in just 1 try! You're best in this game") else: ctr = 0 while (n != num): ctr += 1 count = 0 n = str(n) num = str(num) correct = [] for i in range(0, 4): if (n[i] == num[i]): count += 1 correct.append(n[i]) else: continue # when not all the digits are guessed correctly. if (count < 4) and (count != 0): print("Not quite the number. But you did get ", count, " digit(s) correct!") print("Also these numbers in your input were correct.") for k in correct: print(k, end=' ') print('\n') print('\n') n = int(input("Enter your next choice of numbers: ")) # when none of the digits are guessed correctly. elif (count == 0): print("None of the numbers in your input match.") n = int(input("Enter your next choice of numbers: ")) if n == num: print("You will be best on this game!") print("It took you", ctr, "tries to be a winner of this game.")

OUTPUT:

Submitted by Anita Suresh Lawate (Anitacodespeedy)

Download packets of source code on Coders Packet

## Comments