如果第一次点击按钮,我如何触发操作,但如果再次按下按钮,我将做额外的事情。
- 这是对我目标的描述,我将在下面提供一个小代码
现在,当我从客户端单击按钮时,我需要obj_list
数据进行相乘,并将结果存储在obj_db
中(我已经这样做了(。
但是
当我再次单击按钮时,我需要将从obj_list
返回的新数据与obj_db
中以前存储的数据相乘并求和,依此类推!!
# if i press a button on the client side, (obj_list) returns something like this [0.3, 2.32, 1.22],which i get through python.
if request.method == 'POST':
x = 1
obj_db = []
for obj_items in obj_list:
should_sum = False
while not should_sum:
x *= obj_items
#return only two decimal numbers after float number
new_obj_num = round(obj_total, 2)
obj_db.append(new_obj_num)
print('stored value : ', obj_db)
if object_selected:
obj_db += obj_items
else:
should_sum = True
有什么建议吗??!
正如我们在评论中所说的,您需要保存计数器才能知道有多少人点击了。
# try to read the counter stored in a file
try:
f = open("counter.txt", "r")
data_returned_as_str = f.read()
counter = int(data_returned_as_str)
except FileNotFoundError as e:
counter = 0
# do your stuff
counter += 1
f = open("counter.txt", "w") # use write to overwrite the last content
f.write(str(counter))
f.close()
print(counter)
如果你还需要以前的数据,写另一个文件来读取。