【发布时间】:2016-03-24 14:01:04
【问题描述】:
我的代码有问题,我得到以下输出:
回溯(最近一次通话最后一次):
文件“1stHour.py”,第 48 行,在
ws1.cell(column=1, row=i, value="%s" % blue_student_list[i])
IndexError: 列表索引超出范围`
# coding=utf:8
from pythonzenity import Message
from openpyxl.styles import PatternFill
from openpyxl import Workbook
import bluetooth
import time
def student_check(index):
result = bluetooth.lookup_name(blue_address_list[index], timeout=3)
if (result is not None):
return True
else:
return False
blue_student_list = ['Name', 'Name2']
blue_address_list = ['Address', 'Address2']
redFill = PatternFill(start_color='FF0000', end_color='FF0000', fill_type='solid')
greenFill = PatternFill(start_color='00FF00', end_color='00FF00', fill_type='solid')
for i in range(0, len(blue_address_list)):
i = i + 1
ws1.cell(column=1, row=i, value="%s" % blue_student_list[i])
if (student_check(i)):
ws1.cell(column=2, row=i, value="%s" % "Present").fill = greenFill
else:
ws1.cell(column=2, row=i, value="%s" % "Absent").fill = redFill
Message(title="Attendance Checker",text="You can now open the Excel Document on your Desktop", timeout=3000)
我有这个工作,但忘记保存它,所以我没有正确的方法,我以前在这里有它。我能做些什么来防止这个错误?我觉得我忘记了什么或在我的代码上写了 i = i + 1 部分。
【问题讨论】:
-
你不应该做 i = i + 1 因为 i 已经被 for 循环递增并且你的 blue_student_list 没有 len = 3
-
我将其更改为长度为 3,但它仍然无法正常工作...@SumitKumar
-
你为什么要在那里增加 i 的值。它在那里做任何具体的事情吗?
标签: python bluetooth openpyxl pybluez