Home python Does not work the game code "Guess the number" for the bot...

Does not work the game code “Guess the number” for the bot in TG Python Pytelegrambotapi

Author

Date

Category

I can not write a code for playing in guess the number for the bot, I write code in a separate file (not the main thing) to then call it function, here is the game code:

import random
Import Time.
Def Random_Guesesses (Message, Bot):
# Number of attempts to guess
  Gueses_made = 0.
  Guess = 0.
# Get the username from the console entry
  Name = STR (message.chat.first_name)
# Get a random number ranging from 1 to 30
  Number = Random.randint (1, 30)
  Bot.Send_Message (Message.chat.id, '{0}, I guess the number between 1 and 30. Can you guess?'. Format (Name))
  Time.Sleep (1)
# While the user did not exceed the number of allowed attempts - 6
  While Gueses_made & LT; 6:
    Bot.Send_Message (Message.chat.id, "Enter the number")
    Try:
      Guess = Int (Message.Text)
    Except Valueerror:
      Bot.Send_Message (Message.chat.id, "Invalid Enter. Enter the number")
  # Increase the counter number of attempts
    Gueses_made + = 1
    IF Guess & LT; Number:
        Bot.Send_Message (Message.chat.id, 'Your number is less than what I fucked.')
    ELIF GUESS & GT; Number:
      Bot.Send_Message (Message.chat.id, 'Your number more spent by me.')
  IF Guess == Number:
    Bot.Send_Message (Message.chat.id, 'Wow, {0}! You guessed my number, using {1} attempts!'. Format (Name, Gueses_made))
  ELSE:
    Bot.Send_Message (Message.chat.ID, "And I didn't guess! I fucked the number {0} '. Format (Number))

Call function comes from the main file code:

if message.text.lower () == "Guess the number":
  Bot.Send_Message (Message.chat.id, Conf_Games.random_Guesesses (Message, Bot))

Bot goes around the cycle without taking messages, it seems it is necessary to add the reception of messages from the main file to the cycle, but I do not understand how to do it.
When I am writing a bot “Guess the number” game starts, but the bot writes this, I don’t even have time to answer, it is screaming without stopping:
https://ibb.co/rgqlb2c
In the console, this is:
https://ibb.co/pjlp2sf
Help modify that the game would work, thank you in advance!


Answer 1, Authority 100%

Oh, joint efforts:

@ bot.message_handler (Commands = ['Start'])
Def Welcome (Message, WHERE_CALL = NONE):
  if where_call is none:
    Global Number
    Number = Random.randint (1, 30)
    MSG = Bot.Send_Message (Message.chat.id, "Can you guess the number between 1 and 30?")
    ATTEMPT = 1.
    Bot.register_next_Step_Handler (MSG, Lambda Message: Random_Number (Message, Attempt))
  Elif where_call == 'not_digit':
    MSG = Bot.Send_Message (Message.chat.ID, 'only numbers')
    ATTEMPT = 1.
    Bot.register_next_Step_Handler (MSG, Lambda Message: Random_Number (Message, Attempt))
DEF RANDOM_NUMBER (Message, Attempt):
  If message.text.isdigit ():
    N = int (Message.Text)
    IF Attempt & LT; 6:
      ATTEMPT + = 1
      IF N & LT; Number:
        MSG = Bot.Send_Message (Message.chat.id, 'little \ know')
        Bot.register_next_Step_Handler (MSG, Lambda Message: Random_Number (Message, Attempt))
      ELIF N & GT; Number:
        MSG = Bot.Send_Message (Message.chat.id, 'Much \ Noteby')
        Bot.register_next_Step_Handler (MSG, Lambda Message: Random_Number (Message, Attempt))
      ELSE:
        Bot.Send_Message (Message.chat.id, 'guess, with {} attempts!'. Format (Attempt - 1))
    ELSE:
      Bot.Send_Message (Message.chat.id, 'attempts are exhausted \ Nexagified number {0}'. Format (Number))
  ELSE:
    Welcome (Message, WHERE_CALL = 'NOT_DIGIT')
Bot.Polling (None_stop = True, Interval = 0)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions