repl.it
@PYer/

bagelsDeduction.py

Python

Normal version is 3-digits, 10 guesses

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import random, sys
from replit import clear as c
def digits():
  c()
  wordNums = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'teen', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred', 'thousand', 'million']
  print('*****BAGELS DEDUCTION***** (A number guessing game.)')
  print('How many digits?')
  NUM_DIGITS = input()
  for i in range(len(wordNums)):
    if wordNums[i] in NUM_DIGITS:
      print('Please enter a number in number form, not word form. (5 not five)')
      digits()
  if NUM_DIGITS.startswith("-"):
    print('No negative numbers please.')
    digits()
  NUM_DIGITS = int(NUM_DIGITS)
  if NUM_DIGITS > 10:
    print('Please enter a number less that 10.')
    digits()
  return NUM_DIGITS
def guesses():
    print('How many guesses?')
    MAX_GUESS = input()
    MAX_GUESS = int(MAX_GUESS)
    return MAX_GUESS
def getSecretNum():
    numbers = '1 2 3 4 5 6 7 8 9'.split()
    random.shuffle(numbers)
    secretNum = ''
    for i in range(NUM_DIGITS):
        secretNum += str(numbers[i])
    return secretNum

def getClues(guess, secretNum):
    if guess == secretNum:
        return 'You got it!'
    clues = []
    for i in range(len(guess)):
        if guess[i] == secretNum[i]:
            clues.append('Fermi')
        elif guess[i] in secretNum:
            clues.append('Pico')
    if len(clues) == 0:
        return 'Bagels'
    clues.sort()
    return ' '.join(clues)

def isOnlyDigits(num):
    if num == '':
        return False
    for i in num:
        if not i in '0 1 2 3 4 5 6 7 8 9'.split():
            print('Only digits.')
            return False
        if not i in '1 2 3 4 5 6 7 8 9'.split():
            print('Cannot start with 0.')
            return False
        return True

while True:
    NUM_DIGITS = digits()
    MAX_GUESS = guesses()
    secretNum = getSecretNum()
    print('I am thinking of a %s-digit number. Try to guess what it is.' %(NUM_DIGITS))
    print('When I Say:    That Means:')
    print('  Bagels        None of the digits is correct.')
    print('  Pico          One digit is correct but in the wrong position.')
    print('  Fermi         One digit is correct and in the right position.')
    print('The clues are in A-B-C order.')
    print('I have thought up a number. You have %s guesses to get it.' %(MAX_GUESS))
    print("Type 'QUIT' to quit and find the answer.")
    guessesTaken = 1
    while guessesTaken <= MAX_GUESS:
        guess = ''
        while len(guess) != NUM_DIGITS or not isOnlyDigits(guess):
            print('Guess #%s:' %(guessesTaken))
            guess = input()
            if len(guess) != NUM_DIGITS:
                print('Guess a %s-digit number.' %(NUM_DIGITS))
            if guess == 'QUIT':
                print('You have quit the game. The answer was %s.' %(secretNum))
                sys.exit()
        print(getClues(guess, secretNum))
        guessesTaken += 1

        if guess == secretNum:
            break
        if guessesTaken > MAX_GUESS:
            print('You are out of guesses. The answer was %s.' %(secretNum))

    print('Do you want to play again? (yes or no)')
    yesorno = input()
    c()
    if not yesorno.lower().startswith('y'):
      break