Advertisement
OPiMedia

Very Simple Mastermind

Jan 9th, 2020
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement