![](/img/trans.png)
[英]How do I return or print an attribute that's a math function between two attributes? Python
[英]How to print any math calculation on python under two "for" function?
我是 python 新手,我不明白為什么這段代碼不起作用。 在我嘗試校准 LED 的代碼中,樓層號 + 單元號應分配給 LED 的數量。
floors = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
unitperfloor = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
for i in floors:
for j in unitperfloor:
numberofpixel = unitperfloor[j] + ((floors[i] - 1) * 92)
print("floor:" + str(floors[i]) + " unit: " + str(unitperfloor[j]) + " = " + str(numberofpixel))
我試圖只打印i
, j
並且我看到它正常工作。 但是當我添加這個numberofpixel
計算時, numberofpixel
循環了 1 次。
floors=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
unitperfloor=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in floors:
for j in unitperfloor:
numberofpixel = j + ((i- 1) * 92)
print("floor:{} unit: {} = {}".format(i,j,numberofpixel))
For 獲取列表元素的值,而不是列表的索引。
floors=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
unitperfloor=1, 2, 3, 4, 5, 6, 7, 8, 9, 10
for i in range(len(floors)):
for j in range(len(unitperfloor)):
numberofpixel = unitperfloor[j] + ((floors[i] - 1) * 92)
print("floor:" + str(floors[i]) + " unit: " + str(unitperfloor[j]) + " = " + str(numberofpixel))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.