Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions fizz3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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()