diff --git a/fizz3.py b/fizz3.py index cd42359..bfe7be3 100644 --- a/fizz3.py +++ b/fizz3.py @@ -22,10 +22,11 @@ def print_response(dict): # try an answer and see what fizzbot thinks of it def try_answer(question_url, answer): print_sep() - body = json.dumps({ 'answer': answer }) + body = json.dumps({'answer': answer}) print('*** POST %s %s' % (question_url, body)) try: - req = urllib.request.Request(domain + question_url, data=body.encode('utf8'), headers={'Content-Type': 'application/json'}) + req = urllib.request.Request( + domain + question_url, data=body.encode('utf8'), headers={'Content-Type': 'application/json'}) res = urllib.request.urlopen(req) response = json.load(res) print_response(response) @@ -37,10 +38,36 @@ def try_answer(question_url, answer): print_response(response) return response +def getAnswer(question_data): + rules = question_data['rules'] + numbers = question_data['numbers'] + number = [] + response = [] + + for rule in rules: + number.append(rule['number']) + response.append(rule['response']) + + for i in range(len(numbers)): + res = '' + for j in range(len(number)): + if(not numbers[i] % number[j]): + res += response[j] + if(res != ''): + numbers[i] = res + + return(" ".join([str(x) for x in numbers])) + + # keep trying answers until a correct one is given -def get_correct_answer(question_url): +def get_correct_answer(question_url, question_data, firstTwo): while True: - answer = input('Enter your answer:\n') + if(firstTwo): + answer = 'python' + else: + answer = getAnswer(question_data) + # answer = input('Enter your answer:\n') + print(answer) response = try_answer(question_url, answer) @@ -49,28 +76,33 @@ def get_correct_answer(question_url): exit() if (response.get('result') == 'correct'): - input('press enter to continue') + # input('press enter to continue') return response.get('nextQuestion') # do the next question -def do_question(domain, question_url): +def do_question(domain, question_url, firstTwo): print_sep() print('*** GET %s' % question_url) - request = urllib.request.urlopen( ('%s%s' % (domain, question_url)) ) + request = urllib.request.urlopen(('%s%s' % (domain, question_url))) question_data = json.load(request) print_response(question_data) print_sep() next_question = question_data.get('nextQuestion') - if next_question: return next_question - return get_correct_answer(question_url) + if next_question: + return next_question + return get_correct_answer(question_url, question_data, firstTwo) def main(): question_url = '/fizzbot' + question_url = do_question(domain, question_url, True) + question_url = do_question(domain, question_url, True) while question_url: - question_url = do_question(domain, question_url) + question_url = do_question(domain, question_url, False) + -if __name__ == '__main__': main() +if __name__ == '__main__': + main()