如何打印出列表中的总元素。 我需要一个函数,将打印出在val_holder列表中的总元素,它应该放在代码中的什么地方,如果我能得到完整的代码实现,我将不胜感激
import kivy
from kivy.app import App
from kivy.uix.floatlayout import Floatlayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.label import Label
val_holder=[]
class LandingScreen(FloatLayout):
def __init__(self, **kwargs):
super(LandingScreen, self).__init__(**kwargs)
# put whatever pos_hint value you want.
self.btn1=Button(text='button1 ', size_hint=(0.5, 0.5), on_press=self.click_b1))
self.btn2=Button(text='button1 ', size_hint=(0.5, 0.5), on_press=self.click_b2))
self.btn3=Button(text='button1 ', size_hint=(0.5, 0.5), on_press=self.click_b3))
self.add_widget(self.btn1)
self.add_widget(self.btn2)
self.add_widget(self.btn3)
def click_b1(self, instance):
val_holder.append('a')
def click_b2(self, instance):
val_holder.append('b')
def click_b3(self, instance):
val_holder.append('c')
# I need a function to print out the total element in the val_holder list
class SplashApp(App):
def build(self):
return LandingScreen()
if __name__ == '__main__':
SplashApp().run()
要打印列表中的所有项目,只需执行以下操作:
print(val_holder)
要查找列表中的元素数量,只需执行以下操作:
print(len(val_holder))
如果len()函数有什么问题,那么试试这段代码:
length = 0
for i in val_holder:
length += 1
print(length)