N
N
newbie632021-07-13 12:45:12
Python
newbie63, 2021-07-13 12:45:12

How can a bot not write some lines?

I have a sqlite3 database where the user's numbers are stored (money; bank; box; lvl; xp) The bot displays the numbers with this code:

@dp.message_handler(commands=['number'])
async def number(message: types.Message):
    conn = sqlite3.connect('db.db')
    cur = conn.cursor()
    user_id = message.from_user.id
    result = cur.execute("SELECT * FROM users WHERE user_id = ?", (user_id, )).fetchone()
    money = result[5]
    bank = result[6]
    box = result[7]
    lvl = result[8]
    xp = result[9]
    money1 = (f"У вас money: {money}")
    bank1 = (f"У вас bank: {bank}")
    box1 = (f"У вас box: {box}")
    lvl1 = (f"У вас lvl: {lvl}")
    xp1 = (f"У вас xp: {xp}")
    await message.reply(f"{money1}  {bank1}  {box1}  {lvl1}  {xp1}")

But when one or more values ​​\u200b\u200b= 0, then they do not need to be shown. For example box and xp = 0, then the bot should display
await message.reply(f"{money1} {bank1} {lvl1} ")

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vindicar, 2021-07-13
@newbie63

Make a routine, at least.

def format_non_zero(items):
    message_list = []
    for format, value in items:
        if value != 0:
            message_list.append(format.format(value))
    return message_list

msg_parts = format_non_zero([ #каждый элемент списка - отдельное значение+его формат. Порядок тот же.
    ("У вас money: {0}", money), #обрати внимание на строку формата!
    ("У вас bank: {0}", bank),
    ("У вас brain: {0}", brain),
]) #функция вернёт список строк
await message.reply('\n'.join(msg_parts)) #соединяем строки перед отправкой.

A
alternativshik, 2021-07-13
@alternativshik

read something on the basics of programming. At least a school textbook

A
Afafks123132132165, 2021-07-13
@Afafks123132132165

if(result == 0):
await message.reply(f"{money1} {bank1} {lvl1} ") Like
this?
Well, something like that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question