-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbagels.py
74 lines (60 loc) · 2.23 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
73
74
import random
import pyinputplus as pyip
NUM_DIGITS = 3
MAX_GUESS = 10
def getSecretNum():
# Returns a string of unique random digits that is NUM_DIGITS long.
numbers = list(range(10))
random.shuffle(numbers)
secretNum = ''
for i in range(NUM_DIGITS):
secretNum += str(numbers[i])
return secretNum
def getClues(guess, secretNum):
# Returns a string with the Pico, Fermi, and Bagels clues to the user.
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):
# Returns True if num is a string of only digits. Otherwise returns False.
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('I am thinking of a %s-digit number. Each digit is unique. Try to guess what it is.' %(NUM_DIGITS))
print('The clues I give are...')
print('When I say: That means:')
print(' Bagles 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 = getSecretNum()
print('I 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 isOnlyDigits(guess):
print('Guess #%s: ' % (guessesTaken))
guess = input()
print(getClues(guess, secretNum))
guessesTaken += 1
if guess == secretNum:
break
if guessesTaken > MAX_GUESS:
print('You ran out of guesses. The answer was %s.' % (secretNum))
if pyip.inputYesNo('Do you want to play again? (yes or no): ') == 'no':
break
# print('Do you want to play again? (yes or no)')
# if not input().lower().startswith('y'):
# break