# Python Program for Gama – A Deductive Logic Game

Python Program for Gama – A Deductive Logic Game. This is for mid-level python programmers who have basic knowledge of Python.

## Python Program for Gama – A Deductive Logic Game

In this Game, Guess of a N(Input)-digit number with no repeated digits has to be taken.
Where Clues comes written : It means
alhpa One digit is correct but in the wrong position.
beta One digit is correct and in the correct position.
Gama No digit is correct.
For example, Suppose 123 is the secret number and guessed number is 326 then it will show
the clues as beta alhpa.

### Step By Step Guide :

Step 1 : It stores the secret number the player needs to guess.
Step 2: Keep on looping until correct guess is made.
Step 3: Player is asked whether they want to play again.
Step 4: A string is returned which is made up of numberofdigits of unique random digits.
Step 5: Returns a string with the alhpa, beta, Gama clues for a guess and secret number pair.
Step 6: Clues are given according to the input.

### Source Code: Python Program for Gama – A Deductive Logic Game

``````import random
numberofdigits = 3 # number of digits can be set
maximumguesses = 10 # number of guesses or chance.

def main():
print('''Gama, a deductive logic game.
By Archit

Guess of a {}-digit number with no repeated digits.

When Clues comes written : It means
alhpa One digit is correct but in the wrong position.
beta One digit is correct and in the correct position.
Gama No digit is correct.

For example,Suppose 123 is the secret number and guessed number is 326 then it will show
the clues as beta alhpa.'''.format(numberofdigits))
while True: # Game main loop.
# It stores the secret number the player needs to guess:
secretNum = getSecretNum()
print('Think up of a number THAT CAN BE SECRET NUMBER.')
print(' You have {} guesses to know the secret number.'.format(maximumguesses))
numberofguesses = 1
while numberofguesses <= maximumguesses:
guess = ''
# Keep on looping until correct guess is made:
while len(guess) != numberofdigits or not guess.isdecimal():
print('Guess number #{}: '.format(numberofguesses))
guess = input('> ')

clues = getClues(guess, secretNum)
print(clues)
numberofguesses += 1
if guess == secretNum:
break # They're correct, so break out of this loop.
if numberofguesses > maximumguesses:
print('You ran out of guesses.')

# Player is asked  whether  they want to play again.
print('Do you want to play again? (yes or no)')
if not input('> ').lower().startswith('y'):
break
print('Thanks for playing!')

def getSecretNum():
#A string is returned which is made up of  numberofdigits of unique random digits.
numbers = list('0123456789') # list is created of digits from 0 to 9.
random.shuffle(numbers) # It is shuffle in random order of secret number.

# Getting the first numberofdigits digits in the list for the secret number(i.e generating secret number:
secretNum = ''
for i in range(numberofdigits):
secretNum += str(numbers[i])
return secretNum

def getClues(guess, secretNum):
#Returns a string with the alhpa, beta, Gama clues for a guess and secret number pair
if guess == secretNum:
return 'congratulation it  is correect number, You have answered it perfectly!'
clues = []

for i in range(len(guess)):
if guess[i] == secretNum[i]:
# One of the correct digit is in the correct place.
clues.append('beta')
elif guess[i] in secretNum:
# One of the  correct digit is in the incorrect place.
clues.append('alhpa')
if len(clues) == 0:
return 'Gama' # There are none of the  correct digits at all.
else:
# The clues are being sorted in alphabetical order, That is why it willn't give information away.
clues.sort()
# Making  a single string from the list of string clues.
return ' '.join(clues)

# If the program is run (instead of imported), the game is run:
if __name__ == '__main__':
main()
``````

## Output:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32

>>>

RESTART: C:/Users/ARCHIT/Desktop/new python progrm/Gama, a deduction game.py

Gama, a deductive logic game.

By Archit

Guess of a 3-digit number with no repeated digits.

When Clues comes written : It means

alhpa One digit is correct but in the wrong position.

beta One digit is correct and in the correct position.

Gama No digit is correct.

For example,Suppose 123 is the secret number and guessed number is 326 then it will show

the clues as beta alhpa.

Think up of a number THAT CAN BE SECRET NUMBER.

You have 10 guesses to know the secret number.

Guess number #1:

> 123

alhpa beta

Guess number #2:

> 456

beta

Guess number #3:

> 124

alhpa beta

Guess number #4:

> 120

alhpa beta

Guess number #5:

> 021

alhpa alhpa

Guess number #6:

> 012

alhpa beta

Guess number #7:

> 102

beta beta

Guess number #8:

> 152

congratulation it  is correct number, You have answered it perfectly!

Guess number #9:

>

## Conclusion :

The program is running fine.

This is for mid-level programmers who have basic knowledge of Python. This game can be modeled for different number of guesses and number of digits. For more programs like this click on the link below:

Python Programs Archives – CS Study