OPiMedia

Very Simple Mastermind

Jan 9th, 2020
170
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3. """
  4. Very Simple Mastermind
  5. """
  6.  
  7. import random
  8.  
  9.  
  10. LENGTH = 4
  11.  
  12. NB_SYMBOL = 5
  13.  
  14.  
  15. def main():
  16.     """
  17.    Main
  18.    """
  19.     combination = ''.join(str(random.randint(1, NB_SYMBOL))
  20.                           for _ in range(LENGTH))
  21.     # print(combination)  # for debug
  22.     found = False
  23.  
  24.     nb = 0
  25.     while not found:
  26.         nb += 1
  27.         current = (input('Your proposition? ').strip() + '1111')[:LENGTH]
  28.         nb_good = sum((1 if value == combination[i]
  29.                        else 0)
  30.                       for i, value in enumerate(current))
  31.         found = (nb_good == LENGTH)
  32.         # print(current)  # for debug
  33.         print('{} good values, {} bad values'
  34.               .format(nb_good, LENGTH - nb_good))
  35.  
  36.     print('You found the good combination {} in {} steps.'.format(current, nb))
  37.  
  38.  
  39. if __name__ == '__main__':
  40.     main()
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×