def main(): # get new message from queue events = queue.get_queue('IngressQueue') for event in events: d = event.get_body() d = json.loads(d) uid = d['from'] message = d['message'] # lookup user try: curr_user = users.get_user(uid) except ItemNotFound: curr_user = users.EndUser(uid) curr_user.write() print curr_user print curr_user.last_question # process question if curr_user.last_question is None: send_first_question(uid, event, curr_user) break else: question = questions.get_question(curr_user.last_question) for q, v in question.answers.iteritems(): v_json = json.loads(v) print v[1].lower() if message.lower() == v_json.values()[0].lower(): print "response match found" next_question_id = json.loads(question.child_questions[q]) print next_question_id.values()[0] # create response message & publish to queue next_question = questions.get_question( next_question_id.values()[0]) queue.put_queue( json.dumps({ 'to': uid, 'message': next_question.qstring }), 'EgressQueue') queue.delete_from_queue(event, 'IngressQueue') else: print "unable to find a response match" # invalid response queue.put_queue( json.dumps({ 'to': uid, 'message': "Sorry, that wasn't a valid response" }), 'EgressQueue') queue.delete_from_queue(event, 'IngressQueue') break
def get_new_question(bot, update): """Новый вопрос.""" question = get_question(read_quiz_file(QUESTIONS_FILE)) chat_id = get_chat_id(update.message.chat.id, POSTFIX_TELEGRAM) message_text = switch_to_next_question(chat_id, question, r) update.message.reply_text(message_text) return SOLUTION_ATTEMPT
def main(): # get new message from queue events = queue.get_queue('IngressQueue') for event in events: d = event.get_body() d = json.loads(d) uid = d['from'] message = d['message'] # lookup user try: curr_user = users.get_user(uid) except ItemNotFound: curr_user = users.EndUser(uid) curr_user.write() print curr_user print curr_user.last_question # process question if curr_user.last_question is None: send_first_question(uid, event, curr_user) break else: question = questions.get_question(curr_user.last_question) for q, v in question.answers.iteritems(): v_json = json.loads(v) print v[1].lower() if message.lower() == v_json.values()[0].lower(): print "response match found" next_question_id = json.loads(question.child_questions[q]) print next_question_id.values()[0] # create response message & publish to queue next_question = questions.get_question(next_question_id.values()[0]) queue.put_queue(json.dumps({'to': uid, 'message': next_question.qstring}), 'EgressQueue') queue.delete_from_queue(event, 'IngressQueue') else: print "unable to find a response match" # invalid response queue.put_queue(json.dumps({'to': uid, 'message': "Sorry, that wasn't a valid response"}), 'EgressQueue') queue.delete_from_queue(event, 'IngressQueue') break
def result(id, question_id, answer_id): question = questions.get_question(question_id)[0] choice = results.get_result(answer_id) choice_text = choice[0] result = "VÄÄRIN" if choice[1]: result = "OIKEIN" return render_template("result.html", result=result, question=question, choice=choice_text, \ id = id, question_id = question_id)
def get_new_question(event, vk_api): """Новый вопрос.""" question = get_question(read_quiz_file(QUESTIONS_FILE)) chat_id = get_chat_id(event.user_id, POSTFIX_VK) message_text = switch_to_next_question(chat_id, question, r) return vk_api.messages.send( user_id=event.user_id, random_id=random.randint(1, 1000), keyboard=get_keyboard(), message=message_text, )
def create_user(session): ''' Creates a user session in the store with a new question, deleting existing session if matched. Returns true if successful, false otherwise. ''' if session in users: users.pop(session) users[session] = {} users[session]["tags"] = {} users[session]["questions"] = [] users[session]["products"] = [] users[session]["num_questions"] = 0 if (not add_question(session, questions.get_question())): users.pop(session) return 0 return 1
def answer_question(session, response): ''' Adds answer to user session's store and populates tags accordingly. Also adds a new question if available. Assumes response is of: "Y" = yes, "M" = maybe, "N" = no. Returns 0 if failed, 1 otherwise. ''' if session in users and (response == "Y" or response == "M" or response == "N"): if len(users[session]["questions"]) > 0: question = users[session]["questions"][-1] users[session]["questions"][-1] = (question[0], question[1], response) tag_weights = questions.get_question_weights(question[0], response) update_tags(session, tag_weights) update_products(session) add_question(session, questions.get_question(users[session])) return 1 return 0
def editform_edit_question(game_id): '''Display the editing page for the submitting question.''' if request.form.get('add_option') or request.form.get('delete_option'): game = games.get_details(game_id) options = [] for field, value in request.form.items(): if 'option_text' in field and value.strip( ) != '' and request.form.get('delete_option') != value: options.append(value.strip()) form_data = request.form form = EditQuestionForm(data=form_data, meta={'locales': ['fi_FI', 'fi']}) field_type_name = gameforms.get_field_type_name( request.form['field_type']) return render_template("edit_question.html", form=form, game=game, options=options, action="/game/" + game_id + "/form/edit/update_question", title="Kysymyksen muokkaus: " + field_type_name) else: game = games.get_details(game_id) question = questions.get_question(request.form["edit_question"]) form_data = { "formquestion_id": question['id'], "field_type": str(question['field_type']), "text": question['text'], "description": question['description'] } form = EditQuestionForm(data=form_data, meta={'locales': ['fi_FI', 'fi']}) field_type_name = gameforms.get_field_type_name(question['field_type']) options = map(lambda d: d['text'], question['options']) return render_template("edit_question.html", form=form, game=game, options=options, action="/game/" + game_id + "/form/edit/update_question", title="Kysymyksen muokkaus: " + field_type_name)
def question(id, question_id): if request.method == "GET": question = questions.get_question(question_id) choicesList = questions.get_choices(question_id) allow = False if users.is_student(users.user_id()): allow = True return render_template("question.html", choices = choicesList, \ question_id=question_id, question = question[0], id = id, allow = allow) if request.method == "POST": course_id = id student_id = users.user_id() question_id = question_id choice_id = request.form["answer"] try: answer_id = questions.new_answer(course_id, student_id, question_id, choice_id) return redirect("/courses/" + str(course_id) + "/question/" + str(question_id) + "/result/" + str(answer_id)) except: return render_template("error.html", message="Jokin epäonnistui vastaamisessa")
import questions qa = questions.get_question() a = tuple(qa[1]) aa = [""] * len(a) q = qa[0] print(q + "\n" + str(len(aa)) + " букв\n") def entry_indices(s): indices = [] for i in range(len(a)): if s == a[i]: indices.append(i) return indices def fill_word(indices): global aa for i in range(len(indices)): aa[indices[i]] = a[indices[i]] print(aa) while tuple(aa) != a: letter = input("Введите букву: ").lower() if aa.count(letter) != 0: print("Такая буква уже есть") continue ind = entry_indices(letter) if len(ind) > 0: