MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / main

Function main

src/gamesbyexample/bagels.py:14–56  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12
13
14def main():
15 print('''Bagels, a deductive logic game.
16By Al Sweigart al@inventwithpython.com
17
18I am thinking of a {}-digit number with no repeated digits.
19Try to guess what it is. Here are some clues:
20When 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
25For example, if the secret number was 248 and your guess was 843, the
26clues 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
59def getSecretNum():

Callers 1

bagels.pyFile · 0.70

Calls 2

getSecretNumFunction · 0.85
getCluesFunction · 0.70

Tested by

no test coverage detected