V
V
Viktor2021-07-05 09:27:01
Python
Viktor, 2021-07-05 09:27:01

I'm here on bydlokodil, is it possible to optimize?

Greetings,
I wrote a bot-casino, everything seems to work, but something tells me that the code is too big for little functionality.
I would be very grateful for comments from the "for stupid" series.

the actual code itself:

import telebot
from random import randint

bot = telebot.TeleBot('token')


@bot.message_handler(content_types=['text'])
def handle_text(message):
    if message.text in ['казино', 'Казино']:
        with open('pot.txt') as pot:
            bank = int(pot.read())
        if bank == 0:
            bot.reply_to(message, 'У тебя ноль на балансе, ты не сможешь больше играть')
            return False
        else:
            bet = bot.reply_to(message, 'Сколько ставишь?')
            bot.register_next_step_handler(bet, get_bet)


def new_game(message):
    if message.text in ['да', 'ДА', 'Да']:
        answer = bot.reply_to(message, 'Сколько ставишь?')
        bot.register_next_step_handler(answer, get_bet)
    else:
        bot.reply_to(message, 'Спасибо за игру, приходите к нам снова!')


def get_bet(message):
    bet = int(message.text)
    with open('pot.txt') as pot:
        bank = int(pot.read())
        a = randint(1, 100)
        if bank < bet:
            error = bot.reply_to(message, 'Ты не можешь ставить больше того что у тебя есть! \nТвой банк: ' + str(bank) + '\nВведи другую ставку!')
            bot.register_next_step_handler(error, get_bet)
            # continue
            return message.text
        else:
            if a > 50:
                bank += bet
                result = 'Выпало ' + str(a) + ', ты выиграл ' + str(bet) + ' мои поздравления!\nБанк ' + str(bank)
                bot.reply_to(message, result)
            else:
                bank -= bet
                result = 'Выпало ' + str(a) + ', ты потерял ' + str(bet) + ' попробуй еще\nБанк ' + str(bank)
                bot.reply_to(message, result)
        if int(bank) == 0:
            bot.reply_to(message, 'Ты остался без штанов, тебя выгнали')
            potw = open('pot.txt', 'w')
            potw.write(str(bank))
            potw.close()
            return False
        bot.reply_to(message, 'Сыграем еще? (да/нет):')


    bot.register_next_step_handler(message, new_game)

    potw = open('pot.txt', 'w')
    potw.write(str(bank))
    potw.close()



bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2021-07-05
@Badmajor

Here is this:

if message.text in ['казино', 'Казино']:

if message.text.lower() == "казино":

or such
result = 'Выпало ' + str(a) + ', ты выиграл ' + str(bet) + ' мои поздравления!\nБанк ' + str(bank)
result = f'Выпало {a}, ты выиграл {bet} - мои поздравления!\nБанк {bank}'

well, they wrote it correctly above, there is nothing to "optimize" here, there are five lines of code, logic too.

H
HemulGM, 2021-07-05
@HemulGM

There is nothing to "optimize" here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question