type up bagels.py from book

This commit is contained in:
cheeks 2025-03-11 19:27:24 -04:00
parent f5be8b1fec
commit 534d6ba5da

87
bagels.py Normal file
View File

@ -0,0 +1,87 @@
"""Bagels, by Al Sweigart """
import random
NUM_DIGITS = 3 # (!) Try setting this to 1 or 10
MAX_GUESSES = 10 # (!) Try setting this to 1 or 100
def main():
print('''Bagels a deductive logic game.
By Al Swiegart
I am thinking of a {}-digit number with no repeated digits.
Try to guess what it is. Here are some clues:
When I say: That means:
Pico One digit is correct but in the wrong position.
Fermi One digit is correct and in the right position.
Bagels No digit is correct.
For example, if the secret number was 248 and your guess was 843, the
clues would be Fermi Pico.'''.format(NUM_DIGITS))
while True:
#this stores the secret number the player needs to guess:
secretNum = getSecretNum()
print('I have thought up a number.')
print(' You have {} guesses to get it.'.format(MAX_GUESSES))
numGuesses = 1
while numGuesses <= MAX_GUESSES:
guess = ''
#keep looping until they enter a valid guess
while len(guess) != NUM_DIGITS or not guess.isdecimal():
print('Guess #{}: '.format(numGuesses))
guess = input('> ')
clues = getClues(guess, secretNum)
print(clues)
numGuesses += 1
if guess == secretNum:
break # They're correct, so break out of this loop
if numGuesses > MAX_GUESSES:
print('You ran out of guesses.')
print('The answer was {}.'.format(secretNum))
# ask player if they want to play again
print('Do you want to play again? (yes or no)')
if not input('> ').lower().startswith('y'):
break
print('Thanks for playing')
def getSecretNum():
"""Returns string of NUM_DIGITS unique random digits."""
numbers = list('0123456789')
random.shuffle(numbers)
# Get first NUM_DIGITS in the list for the secret number:
secretNum = ''
for i in range(NUM_DIGITS):
secretNum += str(numbers[i])
return secretNum
def getClues(guess, secretNum):
"""Returns pico, fermi, bagels clues for guesses."""
if guess == secretNum:
return "You got it!"
clues = []
for i in range(len(guess)):
if guess[i] == secretNum[i]:
# A correct digit is in the correct place.
clues.append('Fermi')
elif guess[i] in secretNum:
# A correct digit is in the incorrect place.
clues.append('Pico')
if len(clues) == 0:
return 'Bagels' # There are no correct digits at all.abs
else:
# Sort clues into alphabetical order to change order of clues
clues.sort()
# Make single string from clues:
return ' '.join(clues)
# If the program is run instead of imported, run the game:
if __name__ == '__main__':
main()