A
A
Artur2020-05-22 12:27:29
Python
Artur, 2020-05-22 12:27:29

How not to send a message if the variable is equal to 0?

How to make it so that if the random selects 0, then the message will not be sent. And if he selects 'wires', the message will be sent.

word = ["0", "0", "0", "0", 'провода']

              elif word == 'да':
                if event.from_user: #Если написали в ЛС
                    vk.messages.send( #Отправляем сообщение
                            user_id=event.message.from_id,
                            random_id=get_random_id(),
                            message=random.choice(word) r
                    )
                if event.from_chat:#Если написали в Беседе
                    vk.messages.send( #Отправляем сообщение
                            chat_id=event.chat_id, #Здесь chat_id
                            random_id=get_random_id(),
                            message=random.choice(word) 
                    )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WolfInChains, 2020-05-22
@rackev

Something like that? If 0 drops out, then it simply displays the message to the console, but does not send it to the user, if the wires, then it sends the message. Caps indicated what to put where

import vk_api
import random
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType

vk = vk_api.VkApi(token="ВАШ ТОКЕН")
vk._auth_token()
vk.get_api()
longpoll = VkBotLongPoll(vk, АЙДИ ГРУППЫ) 


def send_msg(peer_id: int, message: str, attachment: str = ""):
    return vk.method("messages.send", {**locals(), "random_id": 0})

while True:
    try:
       for event in longpoll.listen():
           if event.type == VkBotEventType.MESSAGE_NEW:
               if event.object.peer_id == event.object.from_id:   #Только для лс с группой, чтобы работало в беседе == замените на !=
                   if event.object.text == "Рандом":
                       word = "0", "0", "0", "0", 'провода'
                       choice = random.choice(word)
                       if choice == "провода":
                           send_msg(event.obj.peer_id,  f'{choice}')
                       else:
                           print ("Сообщение не отправлено")
    except Exception as e:
        print(repr(e))

5ec7ecf84e7ff543875675.png5ec7ed0b4f5c4379270149.png

Z
zexer, 2020-05-22
@zexer

import random

word = ["0", "0", "0", "0", 'провода']

if random.choice(word) == '0':
    print('Ноль')
else:
    print('Не ноль')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question