D
D
devolw2022-04-21 20:17:18
Python
devolw, 2022-04-21 20:17:18

Python Vk Bot (millionaire), what to fix to work correctly?

The principle of operation of the bot is as follows, there is a csv file with questions/answers, a dictionary is created from the file, a random string is selected from the dictionary, and then the selected random string is processed.
Can't implement function if user answers correctly - next question is asked (15 questions per game) + each new question was different from the previous one

Here is my code

import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
import csv
import random

session = vk_api.VkApi(token="TOKEN")

# Функция отправки сообщения
def sendMessage(user_id, message):
    session.method("messages.send", {
        "user_id": user_id,
        "message": message,
        "random_id": 0
    })

# Импортируем csv файл, записываем его в словарь

with open('millionerQuestions(1).csv', mode='r', encoding='utf-8-sig') as f:
    reader = csv.DictReader(f, delimiter=";")
    millionerList = list(reader)
    randomLine = random.choice(millionerList)

for event in VkLongPoll(session).listen():
    if event.type == VkEventType.MESSAGE_NEW and event.to_me:
        text = event.text
        user_id = event.user_id

        if text == "/start":
            sendMessage(user_id, "Здравствуйте! Хотите сыграть в игру?")
        if text == "Нет":
            sendMessage(user_id, "Жаль :( Когда надумаете напишите команду /start")

        if text == "Да":
            sendMessage(user_id, "Начнем!")

            # Из рандомной строки словаря выводим вопрос
            sendMessage(user_id, randomLine["Question"])

            # Из этой же строки словаря выводим варианты ответов
            sendMessage(user_id, randomLine["allAnswer"])
            sendMessage(user_id, 'Введите ваш ответ: ')

           # Сравниваем ответ юзера с правильным ответом из словаря
            if text == (randomLine["correctAnswer"]):
                sendMessage(user_id, 'Совершенно верно!')
            if text != (randomLine["correctAnswer"]):
                sendMessage(user_id, 'Вы проиграли :(')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question