简体   繁体   English

如何跟踪while循环中由函数调用产生的编号变量。 那里有多少?

[英]How to keep track of numbered variables made from function call in while loop. How many are there?

So I'm writing a small project using python, But now I'm in trouble. 因此,我正在使用python编写一个小项目,但是现在遇到了麻烦。

I made some code like this: 我做了一些这样的代码:

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

So, this will parse something like this (unitl the tuple ends): 因此,这将解析如下内容(将元组结尾):

itembtn0 = types.KeyboardButton(START_BUTTONS[0])
itembtn1 = types.KeyboardButton(START_BUTTONS[1])

and... 和...

So those variables are usable later. 因此,这些变量以后可用。

But, my problem is here. 但是,我的问题在这里。 I want to check how many of those variables are there (itembtn0 itembtn1 itembtn2 itembtn3...) and put them like this: 我想检查一下其中有多少个变量(itembtn0 itembtn1 itembtn2 itembtn3 ...),然后将它们像这样放置:

markup.row(itembtn0, itembtn1, itembtn2)

so , if there were 5 of those, it will return something like this: markup.row(itembtn0, itembtn1, itembtn2, itembtn3, itembtn4) 因此,如果其中有5个,它将返回如下内容:markup.row(itembtn0,itembtn1,itembtn2,itembtn3,itembtn4)

Actually I have no idea for what I should write. 其实我不知道该写些什么。

Thanks for help! 感谢帮助! & Sorry for my bad english. & 对不起,我的英语不好。

You are trying to create numbered variables, which can in all cases be replaced by an array. 您正在尝试创建带编号的变量,在所有情况下都可以将其替换为数组。 Try something simple instead: 尝试一些简单的方法:

START_BUTTONS = ("button1", "button2")
markup = types.ReplyKeyboardMarkup()
itembtn = []
for btn in START_BUTTONS:
  itembtn.append(types.KeyboardButton(btn))

Access it with 通过访问

itembtn[0]
itembtn[1]
etc.

And you can know how many there are: 您会知道有多少:

len(itembtn)

I am not sure about your markup function, but you can pass the whole array as parameters like this: 我不确定您的标记功能,但是您可以像这样将整个数组作为参数传递:

markup.row(*itembtn)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM