S
S
Studentka19962020-02-07 19:34:05
Python
Studentka1996, 2020-02-07 19:34:05

How to add one with each click on the button?

Hello. How to add one with each click on the button?

I wrote a not very correct "algorithm" in order to write a new value to the list and read an element from the new list .. But it doesn't work like that either, the code processes the original value with each click. what is wrong in the code?

Исходные значения: список с элементом 1, i переменная ссылкой на 1
Записываю новое значение в список и читаю последний элемент [-1], так как append добавляет элемент в конец списка. Конечно, можно и другие методы использовать, не суть. В If условии проверка двух значений
 if text == '+1':
        new_i = [1]
        i=1
        if new_i[-1] >= i:
            i += 1
            bot.send_message(message.chat.id, i)
            new_i.append(i)
            print(new_i[-1])
        print(new_i)

5e3d907a72ac0069100076.png
I understand that with a list, perhaps debility, but it’s even more unclear how to use the new value in a different way.
I experimented a little with the for loop, but as I understand it, the loop is needed for output.

The result is needed: Click 1: 2, Click 2: 3 ... Click n: n

Solved the problem, immediately directly write to the database ..
if text == '+1':
        cur.execute(f'SELECT Количество FROM Корзина_юзера  WHERE  Ключ_юзера = {message.chat.id}')
        show_Kol = cur.fetchone()
        #Увеличить кол-во на единицу
        new_Kol = int(show_Kol[0]) +1
        print(str(new_Kol) )
        #Обновить таблицу Корзина_юзера, установив новое значение
        cur.execute(f'''UPDATE Корзина_юзера  SET Количество = {str(new_Kol) }
        WHERE Товар LIKE '%' AND Ключ_юзера = {message.chat.id}''')
        con.commit() #Записать в БД

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sergeyfilippov4, 2020-02-07
@sergeyfilippov4

clicks = 0
if text == "+1":
    clicks += 1

D
Dzhemchik, 2020-02-07
@dzhem911

x = 1
y = []
something = 'something'
if something == 'something':
    x += 1
    y.append(x)
    print(y)
print(x)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question