[英]How to keep track of numbered variables made from function call in while loop. How many are there?
因此,我正在使用python编写一个小项目,但是现在遇到了麻烦。
我做了一些这样的代码:
START_BUTTONS = ("button1", "button2")
markup = types.ReplyKeyboardMarkup()
lengthof = len(START_BUTTONS)
countn = 0
while (countn < lengthof):
exec("itembtn" + str(countn) + " = types.KeyboardButton(START_BUTTONS[" + str(countn) + "])")
countn = countn + 1
因此,这将解析如下内容(将元组结尾):
itembtn0 = types.KeyboardButton(START_BUTTONS[0])
itembtn1 = types.KeyboardButton(START_BUTTONS[1])
和...
因此,这些变量以后可用。
但是,我的问题在这里。 我想检查一下其中有多少个变量(itembtn0 itembtn1 itembtn2 itembtn3 ...),然后将它们像这样放置:
markup.row(itembtn0, itembtn1, itembtn2)
因此,如果其中有5个,它将返回如下内容:markup.row(itembtn0,itembtn1,itembtn2,itembtn3,itembtn4)
其实我不知道该写些什么。
感谢帮助! & 对不起,我的英语不好。
您正在尝试创建带编号的变量,在所有情况下都可以将其替换为数组。 尝试一些简单的方法:
START_BUTTONS = ("button1", "button2")
markup = types.ReplyKeyboardMarkup()
itembtn = []
for btn in START_BUTTONS:
itembtn.append(types.KeyboardButton(btn))
通过访问
itembtn[0]
itembtn[1]
etc.
您会知道有多少:
len(itembtn)
我不确定您的标记功能,但是您可以像这样将整个数组作为参数传递:
markup.row(*itembtn)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.