def start_quiz(update, context): ''' Starts the quiz and gets the questions from aloc API, then saves to database ''' chat_id = update.effective_chat.id message = update.message.text try: name = update["message"]["chat"]["first_name"] except: name = update["message"]["chat"]["username"] subject = message.split('_')[1] questions = get_questions(subject=subject, mode='utme') quiz = client.query( q.create( q.collection("quiz"), { "data": { "user_id": chat_id, "subject": subject, "answered": 0, "score": 0, "id_": random.randint(0, 100), 'completed': False, 'questions': questions, } })) user = client.query(q.get(q.match(q.index("users"), chat_id))) client.query( q.update(q.ref(q.collection("users"), user["ref"].id()), {"data": { "last_command": "start_quiz" }})) msg = f"{subject.upper()}\n\n10 questions, enter 'q' to continue\n\n/end_quiz to end the quiz session." context.bot.send_message(chat_id=chat_id, text=msg)
def get_quiz_questions(): subject = request.args.get('subject') num_questions = request.args.get('num') sources = request.args.getlist('source') topics = request.args.getlist('topic') if num_questions is not None: num_questions = int(num_questions) return json.dumps( questions.get_questions(subject, num_questions, sources=sources, topics=topics))
print(question.questionText) print("") for answer in question.answers: print(answer) print() keyboard = input("Give me ur answer pls: ") print() if check_answer(question, keyboard): RightAnswers = RightAnswers + 1 else: WrongAnswers = WrongAnswers + 1 # shows the questions for question in questions.get_questions(): show_question(question) # prints how many you got wrong and right print("You got %d correct and got %d wrong" % (RightAnswers, WrongAnswers)) if RightAnswers == 0: print("Dismal.") if WrongAnswers == 0: print("Woo, I guess.") # prints how much you got in a percentage if WrongAnswers == 0: print("You got 100%") if RightAnswers == 0: print("You got 0%") if RightAnswers > 0 and WrongAnswers > 0:
message = textwrap.dedent(f'''\ Удачи, {user}! Чтобы начать сначала, нажми /start''') update.message.reply_text(text=message, reply_markup=ReplyKeyboardRemove()) return ConversationHandler.END if __name__ == '__main__': load_dotenv() logging.basicConfig( level=logging.INFO, format=f'%(asctime)s - [%(levelname)s] <{logger.name}> %(message)s') questions = get_questions() number_of_questions = len(questions) logger.info(f'Всего вопросов: {number_of_questions}') TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN') REDIS_HOST = os.getenv('REDIS_HOST') REDIS_PORT = os.getenv('REDIS_PORT') REDIS_PASSWORD = os.getenv('REDIS_PASSWORD') START_KEYBOARD = [['Новый вопрос']] ANSWER_KEYBOARD = [['Сдаться', 'Мой счет']] PLAY_KEYBOARD = [['Новый вопрос', 'Мой счет']] START_REPLY_MARKUP = ReplyKeyboardMarkup(START_KEYBOARD, resize_keyboard=True)
from questions import get_questions GAME_QS=get_questions() # a function to run a question take a dictionary as an argument def run_question(Q): ''' takes the question dictionary checks user response returns none ''' # get question key from dictionary and ask user to answer user_answer = input(Q["question"]) # check user answer is same as answer key in dictionary if user_answer==Q["answer"]: print("Great work") elif user_answer == "q": return "q" # or print wrong answer message else: print("Better luck next time") def main_game(): counter = 0 while counter<len(GAME_QS): result = run_question(GAME_QS[counter]) if result == "q": return "q" counter += 1 return None
def main(): logger.info('Loading settings from environment') config.load_from_environment() creator = setup.get_creator() setup.update_questions(get_questions(creator)) store.session.commit()