vendredi 19 août 2022

How to make a deductive logic game of guessing a three degit number?

The question is: In Bagels, a deductive logic game, you must guess a secret three-digit number based on clues. The game offers one of the following hints in response to your guess: “Pico” when your guess has a correct digit in the wrong place, “Fermi” when your guess has a correct digit in the correct place, and “Bagels” if your guess has no correct digits. You have 10 tries to guess the secret number.

I am new in Python. Any suggestions are appreciated:

#Generating a random 3-digit number
import random
from random import randint

def random_with_N_digits(n):
    range_start = 10**(n-1)
    range_end = (10**n)-1
    return randint(range_start, range_end)
    
random_number=random_with_N_digits(3)
print(random_number)

#Making a dictionary From the integers of the number
#Indexes as Keys and integers as values
rand_list = [int(x) for x in str(random_number)]
rand_dictionary=dict(zip(range(len(rand_list)),rand_list))
#rand_dictionary

#For loop for 10 tries
for i in range(10):
    input_num = int(input('Please guess that three digit number : '))
    #Validating the input
    if input_num < 999 and input_num > 100:
        #Making dictionary for the inputted number in similar way like the random number
        input_list = [int(x) for x in str(input_num)]
        input_dictionary = dict(zip(range(len(input_list)), input_list))
        if random_number == input_num:
            print("Your answer is correct!!")
            break
        elif [i for (i, j) in zip(rand_list, input_list) if i == j]:
            print("Fermi")
            if set(rand_list) & set(input_list):
                print("Pico")
        elif set(rand_list) & set(input_list):
            print("Pico")
        else:
            print("Bagels")
    else:
        print("This is not a valid three digit number. Please try again.")
print("The test is finished.")



Aucun commentaire:

Enregistrer un commentaire