forked from mooja/dailyprogrammer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge135easy.py
56 lines (41 loc) · 1.14 KB
/
challenge135easy.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
#!/usr/bin/env python
# encoding: utf-8
# Daily Programmer Challenge 135 Easy
#
# http://www.reddit.com/r/dailyprogrammer/comments/1k7s7p/081313_challenge_135_easy_arithmetic_equations/
#
# June.10.2015
import random
def generate_equation_str(lo, hi):
equation_fmt = 'n o n o n o n'
equation = ''
for char in equation_fmt:
if char == 'n':
equation += str(random.randrange(lo, hi+1))
elif char == 'o':
equation += random.choice('+-*')
else:
equation += char
return equation
def main():
try:
lo, hi = raw_input('>').strip().split()
lo, hi = int(lo), int(hi)
except:
return
while True:
try:
equation = generate_equation_str(lo, hi)
user_answer = raw_input('> ' + equation + '\n')
if user_answer == 'q':
return
if int(user_answer) == eval(equation):
print '> Correct!'
else:
print '> Incorrect!'
print
except:
print 'could not parse input.'
return
if __name__ == '__main__':
main()