()
| 12 | |
| 13 | |
| 14 | def main(): |
| 15 | print('''Bagels, a deductive logic game. |
| 16 | By Al Sweigart al@inventwithpython.com |
| 17 | |
| 18 | I am thinking of a {}-digit number with no repeated digits. |
| 19 | Try to guess what it is. Here are some clues: |
| 20 | When I say: That means: |
| 21 | Pico One digit is correct but in the wrong position. |
| 22 | Fermi One digit is correct and in the right position. |
| 23 | Bagels No digit is correct. |
| 24 | |
| 25 | For example, if the secret number was 248 and your guess was 843, the |
| 26 | clues would be Fermi Pico.'''.format(NUM_DIGITS)) |
| 27 | |
| 28 | while True: # Main game loop. |
| 29 | # This stores the secret number the player needs to guess: |
| 30 | secretNum = getSecretNum() |
| 31 | print('I have thought up a number.') |
| 32 | print(' You have {} guesses to get it.'.format(MAX_GUESSES)) |
| 33 | |
| 34 | numGuesses = 1 |
| 35 | while numGuesses <= MAX_GUESSES: |
| 36 | guess = '' |
| 37 | # Keep looping until they enter a valid guess: |
| 38 | while len(guess) != NUM_DIGITS or not guess.isdecimal(): |
| 39 | print('Guess #{}: '.format(numGuesses)) |
| 40 | guess = input('> ') |
| 41 | |
| 42 | clues = getClues(guess, secretNum) |
| 43 | print(clues) |
| 44 | numGuesses += 1 |
| 45 | |
| 46 | if guess == secretNum: |
| 47 | break # They're correct, so break out of this loop. |
| 48 | if numGuesses > MAX_GUESSES: |
| 49 | print('You ran out of guesses.') |
| 50 | print('The answer was {}.'.format(secretNum)) |
| 51 | |
| 52 | # Ask player if they want to play again. |
| 53 | print('Do you want to play again? (yes or no)') |
| 54 | if not input('> ').lower().startswith('y'): |
| 55 | break |
| 56 | print('Thanks for playing!') |
| 57 | |
| 58 | |
| 59 | def getSecretNum(): |
no test coverage detected