-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbagels.py
72 lines (55 loc) · 1.86 KB
/
bagels.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
import random
NUM_DIGITS = 3
MAX_GUESS = 10
def get_secret_num():
numbers = list(range(10))
random.shuffle(numbers)
secret_num = ''
for i in range(NUM_DIGITS):
secret_num += str(numbers[i])
return secret_num
def get_clues(guess, secret_num):
# Returns a string with the Pico, Fermi, & Bagels clues to the user.
if guess == secret_num:
return 'You got it!'
clues = []
for i in range(len(guess)):
if guess[i] == secret_num[i]:
clues.append('Fermi')
elif guess[i] in secret_num:
clues.append('Pico')
if len(clues) == 0:
return 'Bagels'
clues.sort()
return ' '.join(clues)
def is_only_digits(num):
if num == '':
return False
for i in num:
if i not in '0 1 2 3 4 5 6 7 8 9'.split():
return False
return True
print('\nI am thinking of a %s-digit number. Try to guess what it is.' % (NUM_DIGITS))
print('The clues I give are...')
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.')
while True:
secretNum = get_secret_num()
print('\nI have thought up a number. You have %s guesses to get it.' % (MAX_GUESS))
guessesTaken = 1
while guessesTaken <= MAX_GUESS:
guess = ''
while len(guess) != NUM_DIGITS or not is_only_digits(guess):
print('Guess #%s: ' % (guessesTaken))
guess = input()
print(get_clues(guess, secretNum))
guessesTaken += 1
if guess == secretNum:
break
if guessesTaken > MAX_GUESS:
print('You ran out of guesses. The answer was %s.' % (secretNum))
print('\nDo you want to play again? (yes or no)')
if not input().lower().startswith('y'):
break