forked from charleyoshi/nerdle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nerdle.py
123 lines (89 loc) · 3.15 KB
/
nerdle.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import sys
from bake import *
maxAttempt = 6
message = '_ _ _ _ _ _ _ _'
VALIDCHARACTERS = '1234567890+-*/= '
def main():
# If you already have a database, comment the next line
bake(1000)
play('equationdatabase.txt')
def play(db):
'''Take the name of .txt file of the eqauation database and play'''
isWin = False
equationList = []
try:
with open(db) as f:
for line in f:
equationList.append(line.strip())
except:
print('OOOPS! No such file called \'{0}\' in your directory. Make sure you get the right name for your .txt file.'.format(db))
sys.exit()
answer = random.choice(equationList)
currentAttempt = 0
while currentAttempt < maxAttempt:
print('------------------------------------')
print('\tLet\'s guess! You have {0} attempts left.'.format(maxAttempt - currentAttempt))
if currentAttempt == 0:
print(message)
else:
print(userInput)
print(hintstr.join(hint))
isValidInput = False
while isValidInput == False:
userInput = input('Enter your guess:')
if (len(userInput) - userInput.count(' ') == len(answer)):
if userInput.count('=') != 1:
print('Make sure there is only one =')
continue
if isEquationValid(userInput):
isValidInput = True
break
else:
print('Make sure your equation is correct. Maybe you would like to check your math again.')
else:
if(len(userInput) != 0):
print('There are {} spaces!'.format(len(answer)))
userInput = userInput.replace(' ', '')
print(userInput)
if userInput == answer:
isWin = True
break
hintstr = ''
hint = []
for i in range(len(userInput)):
if userInput[i] == answer[i]:
hint.append('o')
elif userInput[i] in answer:
hint.append('s')
else:
hint.append('x')
currentAttempt += 1
if isWin:
# show Win message
print('\tYou won! The answer is {0}'.format(answer))
else:
#show Lose message
print('\tSorry! You lose. You\'ve ran out of attempts')
print('\tThe answer is: {0}'.format(answer))
def isEquationValid(userInput):
for i in userInput:
if i not in VALIDCHARACTERS:
return False
try:
lhs = userInput[:userInput.index('=')].replace(' ', '')
rhs = userInput[userInput.index('=') + 1:].replace(' ', '')
if '+' in rhs or '-' in rhs or '*' in rhs or '/' in rhs :
print('The right hand side of your equation should only contain numbers')
return False
try:
answer = calculate(lhs)
if float(answer) == float(rhs):
return True
except:
return False
except:
# lhs or rhs is None
return False
return False
if __name__ == "__main__":
main()