Y
Y
yedkey2021-12-21 16:37:36
Python
yedkey, 2021-12-21 16:37:36

I want the bot to give out random numbers when the user enters his own how to make random.choice does not help?

import telebot
from telebot import types
import random
import time
from random import choice
TOKEN = 'token'

bot = telebot.TeleBot(TOKEN)

@bot.message_handler(commands=['start'])
def start(message):
markup = types. ReplyKeyboardMarkup(resize_keyboard = True)
item1 = types.KeyboardButton('Inza Weather')
item2 = types.KeyboardButton('Inza News')
item3 = types.KeyboardButton('Numbers Game')
markup.add(item1, item2, item3 )

bot.send_message(message.chat.id, "Hello {0.first_name}!" .format(message.from_user), reply_markup = markup)

number = ['1', '2', '3', '4' , '5', '6']

@bot.message_handler(content_types=['text'])
def bot_message(message):
if message.text == 'Weather in Inza':
bot.send_message(message.chat.id, ' https://www.gismeteo.ru/weather-inza-4427/3-days/ ' )
elif message.text == 'Inza's news':
bot.send_message(message.chat.id, ' https://vk.com/inzaru ' )
elif message.text == 'Number game':
bot.send_message( message.chat.id, 'Enter a number from 1 to 6')
elif message.text == '1':
bot.send_message(message.chat.id, 'My number is:', random.choice(number))
elif message .text == '2':
bot.send_message(message.chat.id, 'My number is:', random.choice(number))
elif message.text == '3':
bot.send_message(message.chat.id, 'My number is:', random.choice(number))
elif message.text == '4':
bot.send_message(message.chat.id, 'My number is:', random .choice(number))
elif message.text == '5':
bot.send_message(message.chat.id, 'My number is:', random.choice(number))
elif message.text == '6':
bot .send_message(message.chat.id, 'My number is:', random.choice(number))
else:
bot.send_message(message.chat.id, 'I don't understand' )

bot.polling(none_stop = True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
desaki, 2021-12-21
@yedkey

It would be more correct like this:

bot.send_message(message.chat.id, f"Мое число: {choice(number)}")

I'll explain why just choice : At the beginning of the code, you imported it, but write random.choice , this is wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question