A
A
Alexander2021-07-07 15:13:26
Python
Alexander, 2021-07-07 15:13:26

How to "reload" the game?

There is a code:

@bot.message_handler(commands=['start'])
def welcome(message):
  bot.send_message(message.chat.id,'Привет! Это игра в цифры. Отгадай загаданное число ботом и получи приз! Пиши /go что бы начать! Удачи!')

@bot.message_handler(commands=['go'])
def welcome(message):
  bot.send_message(message.chat.id,'Погнали! Вводи числа...!')
  test()

lst = []

def test():
  @bot.message_handler(content_types=["text"])
  def start_game(message):
    if message.text == '1':
      bot.send_message(message.chat.id,'Ты победил! Введи /go и поехали снова!')
      lst.clear()
      return
    lst.append(message.text)
    for i in lst:
      bot.send_message(message.chat.id, i)
bot.polling()


The conclusion is:
60e599155f4c1924079659.jpeg

As if guessing the numbers, how to restart the game. After "You win! Type /go and let's go again!" wait for /go commands only. If not /go, then like - Idiot, enter /go and again "Let's go, enter numbers!"
There's something small somewhere, but I can't figure it out...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yaroslav Kudrin, 2021-07-07
@Yarru

First, create a storage class (OOP):

class Data:
  def __init__(self):
    self.lst = []
    self.go = False

In place of lst = [] in your code, put the creation of the store: data = Data()
Next, replace test() with data.go = True in the 2nd welcome
Now just remove the def test() function, leaving def start_game() over bot .polling() :
@bot.message_handler(content_types=["text"])
  def start_game(message):
    if message.text == '1':
      bot.send_message(message.chat.id,'Ты победил! Введи /go и поехали снова!')
      lst.clear()
      return
    lst.append(message.text)
    for i in lst:
      bot.send_message(message.chat.id, i)

Replace return with data.go = False
And add a condition
if data.go:
  # весь код, находящийся в def start_game()
else:
  bot.send_message(message.chat.id, "Напишите /go !")

Into your def start_game() function with replacement.
It seems everything, if something is not clear, please contact.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question